C# 如何通过运行.exe在特定文件夹中生成输出
我正在用C#开发wpf应用程序。我能够运行以下exe文件C# 如何通过运行.exe在特定文件夹中生成输出,c#,wpf,exe,system.diagnostics,C#,Wpf,Exe,System.diagnostics,我正在用C#开发wpf应用程序。我能够运行以下exe文件 public static void GenerateCsvFile(string fileName) { System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagn
public static void GenerateCsvFile(string fileName)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = @"C:\ndfd\degrib\bin\degrib.exe";
startInfo.Arguments = fileName + " -C -msg 1 -Csv";
process.StartInfo = startInfo;
process.Start();
System.Diagnostics.Process process1 = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo1 = new System.Diagnostics.ProcessStartInfo();
startInfo1.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo1.FileName = @"C:\ndfd\degrib\bin\degrib.exe";
startInfo1.Arguments = fileName + " -C -msg all -nMet -Csv";
process1.StartInfo = startInfo1;
process1.Start();
}
以上代码为我成功生成了csv文件。但是.csv文件根据文件名在不同的位置生成。表示每次在不同的文件夹中生成.csv文件。我可以强制exe在特定文件夹中生成.csv文件吗?你能提供我任何代码或链接,通过它我可以解决上述问题吗
编辑:用户选择zip文件并对表单进行sumbit。App.ApplicationPath是硬编码路径。下面是我的代码
private void ShowPointsButton_Click(object sender, RoutedEventArgs e)
{
ZipHelper.UnZip(FileNameTextBox.Text, App.ApplicationPath, safeFileName, 9999999);
}
public static void UnZip(string SrcFile, string DstFile, string safeFileName, int bufferSize)
{
//ICSharpCode.SharpZipLib.Zip.UseZip64.Off;
FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read);
ZipInputStream zipInStream = new ZipInputStream(fileStreamIn); ;
//if (SrcFile.Contains(".bz2"))
//{
//BZip2InputStream zipInStream = new BZip2InputStream(fileStreamIn);
//}
//else
//{
// zipInStream = new ZipInputStream(fileStreamIn);
//}
string rootDirectory = string.Empty;
if (safeFileName.Contains(".zip"))
{
rootDirectory = safeFileName.Replace(".zip", string.Empty);
}
else
{
rootDirectory = safeFileName;
}
Directory.CreateDirectory(App.ApplicationPath + rootDirectory);
while (true)
{
ZipEntry entry = zipInStream.GetNextEntry();
if (entry == null)
break;
if (entry.Name.Contains("/"))
{
string[] folders = entry.Name.Split('/');
string lastElement = folders[folders.Length - 1];
var folderList = new List<string>(folders);
folderList.RemoveAt(folders.Length - 1);
folders = folderList.ToArray();
//string folderPath = "";
//foreach (string str in folders)
//{
//folderPath = folderPath + @"\" + str;
//string blackslash = folderPath.Substring(0, 1);
//if (blackslash == "\\")
//{
// folderPath = folderPath.Remove(0, 1);
//}
//if (!Directory.Exists(App.ApplicationPath + rootDirectory + "/" + folderPath))
//{
// Directory.CreateDirectory(App.ApplicationPath + rootDirectory + "/" + folderPath);
//}
//}
if (!string.IsNullOrEmpty(lastElement))
{
//folderPath = folderPath + @"\" + lastElement;
//string blackslash = folderPath.Substring(0, 1);
//if (blackslash == "\\")
//{
// folderPath = folderPath.Remove(0, 1);
//}
WriteToFile(DstFile + rootDirectory + @"\" + lastElement, bufferSize, zipInStream, rootDirectory, entry);
}
}
else
{
WriteToFile(DstFile + rootDirectory + @"\" + entry.Name, bufferSize, zipInStream, rootDirectory, entry);
}
}
zipInStream.Close();
fileStreamIn.Close();
}
private static void WriteToFile(string DstFile, int bufferSize, ZipInputStream zipInStream, string rootDirectory, ZipEntry entry)
{
WriteFileContents(DstFile, bufferSize, zipInStream);
if (DstFile.Contains(".grb"))
{
Utility.GenerateCsvFile(DstFile);
}
//if(DstFile.Contains(".csv"))
//{
// WriteFileContents(@"D:\Documents" + rootDirectory + @"\" + entry.Name, bufferSize, zipInStream);
//}
}
private static void WriteFileContents(string DstFile, int bufferSize, ZipInputStream zipInStream)
{
FileStream fileStreamOut = new FileStream(DstFile, FileMode.OpenOrCreate, FileAccess.Write);
int size;
byte[] buffer = new byte[bufferSize];
do
{
size = zipInStream.Read(buffer, 0, buffer.Length);
fileStreamOut.Write(buffer, 0, size);
} while (size > 0);
fileStreamOut.Close();
}
private void showpoints按钮点击(对象发送者,路由目标)
{
解压(FileNameTextBox.Text,App.ApplicationPath,safeFileName,999999);
}
公共静态void解压(string SrcFile、string DstFile、string safeFileName、int bufferSize)
{
//ICSharpCode.SharpZipLib.Zip.UseZip64.Off;
FileStream fileStreamIn=newfilestream(SrcFile,FileMode.Open,FileAccess.Read);
ZipInputStream zipInStream=新的ZipInputStream(fileStreamIn);
//if(SrcFile.Contains(“.bz2”))
//{
//BZip2InputStream zipInStream=新的BZip2InputStream(fileStreamIn);
//}
//否则
//{
//zipInStream=新的ZipInputStream(fileStreamIn);
//}
string rootDirectory=string.Empty;
if(safeFileName.Contains(“.zip”))
{
rootDirectory=safeFileName.Replace(“.zip”,string.Empty);
}
其他的
{
rootDirectory=safeFileName;
}
Directory.CreateDirectory(App.ApplicationPath+rootDirectory);
while(true)
{
ZipEntry entry=zipInStream.GetNextEntry();
if(条目==null)
打破
if(entry.Name.Contains(“/”)
{
string[]folders=entry.Name.Split('/');
字符串lastElement=folders[folders.Length-1];
var folderList=新列表(文件夹);
folderList.RemoveAt(folders.Length-1);
folders=folderList.ToArray();
//字符串folderPath=“”;
//foreach(文件夹中的字符串str)
//{
//folderPath=folderPath+@“\”+str;
//string blackslash=folderPath.Substring(0,1);
//如果(黑色斜杠=“\\”)
//{
//folderPath=folderPath.Remove(0,1);
//}
//如果(!Directory.Exists(App.ApplicationPath+rootDirectory+“/”+folderPath))
//{
//Directory.CreateDirectory(App.ApplicationPath+rootDirectory+“/”+folderPath);
//}
//}
如果(!string.IsNullOrEmpty(lastElement))
{
//folderPath=folderPath+@“\”+lastElement;
//string blackslash=folderPath.Substring(0,1);
//如果(黑色斜杠=“\\”)
//{
//folderPath=folderPath.Remove(0,1);
//}
WriteToFile(DstFile+rootDirectory+@“\”+lastElement,bufferSize,zipInStream,rootDirectory,entry);
}
}
其他的
{
WriteToFile(DstFile+rootDirectory+@“\”+entry.Name、bufferSize、zipInStream、rootDirectory、entry);
}
}
zipInStream.Close();
fileStreamIn.Close();
}
私有静态void WriteToFile(字符串DstFile、int bufferSize、ZipInputStream、字符串rootDirectory、ZipEntry条目)
{
WriteFileContent(DstFile、bufferSize、zipInStream);
if(DstFile.Contains(“.grb”))
{
Utility.GenerateCsvFile(DstFile);
}
//if(DstFile.Contains(“.csv”))
//{
//WriteFileContent(@“D:\Documents”+rootDirectory+@“\”+entry.Name、bufferSize、zipInStream);
//}
}
私有静态void writeFileContent(字符串DstFile、int bufferSize、ZipInputStream zipInStream)
{
FileStream fileStreamOut=新文件流(DstFile,FileMode.OpenOrCreate,FileAccess.Write);
整数大小;
字节[]缓冲区=新字节[bufferSize];
做
{
size=zipInStream.Read(buffer,0,buffer.Length);
fileStreamOut.Write(缓冲区,0,大小);
}而(尺寸>0);
fileStreamOut.Close();
}
在上面的代码中,请参见第行Utility.GenerateCsvFile(DstFile);我想在“DstFile”位置生成.csv文件。简而言之,就是我解压缩文件的文件夹,也就是我希望.exe写入.csv文件的文件夹。例如,考虑有D:/XYZ文件夹,其中我解压缩我的ZIP文件。在此文件夹中有test.grib文件。我想运行test.grib的exe并生成.csv文件。我希望将这些.csv文件写入XYZ文件夹。文件名需要指向正确的目录和文件,例如c:\somedir\mycsvfile.csv
string folder = "c:\temp";
string fileName = "c:\somedir\blah\file.csv";
string outputFilePath = Path.Combine(folder, new FileInfo(fileName).Name);
这将得到一个文件路径c:\temp\file.csv。这就是你想要的东西吗?processStartInfo.WorkingDirectory=@“你的目录”
请试试这个 上面的.exe运行并生成D:/Docs中的.csv文件。我想在C:/xyz/test中生成.csv文件,而不是D:/Docs。我该怎么做?D:\Docs值来自哪里?它来自startInfo1.FileName=@“C:\ndfd\degrib\bin\degrib.exe”;startInfo1.Argu