我正在用 C# 开发 wpf 应用程序。我有一个按钮,通过 Microsoft.Win32.OpenFileDialog 浏览文件系统。有一个提交按钮,我在其上调用 Process.Start() 以在 grib 文件上运行 .exe。exe 成功为我生成了 .csv 文件。首先我浏览文件系统,选择文件,然后单击提交按钮。我的应用程序执行路径是 D:\Projects\ApiRouting\ApiRouting\bin\Debug。我的应用程序中有一个文件夹位于 D:\Projects\ApiRouting\ApiRouting\Files。当我从路径 D:\Projects\ApiRouting\ApiRouting\Files 中选择文件并单击提交按钮时,.csv 文件将在 D:\Projects\ApiRouting\ApiRouting\Files 生成,这是正确的。当我从 D:\Documents 中选择文件并单击提交按钮时,会在 D:\Documents 中生成 .csv 文件。我要运行的代码。
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";
startInfo.UseShellExecute = true;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
process.Close();
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";
startInfo1.UseShellExecute = true;
process1.StartInfo = startInfo1;
process1.Start();
process1.WaitForExit();
process1.Close();
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
safeFileName = string.Empty;
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
//dlg.DefaultExt = ".txt";
//dlg.Filter = "Zip Files|*.zip*";
dlg.Multiselect = false;
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
FileNameTextBox.Text = string.Empty;
// Open document
string fileName = dlg.FileName;
safeFileName = dlg.SafeFileName;
App.ZipFileSafeName = safeFileName;
FileNameTextBox.Text = fileName;
App.ZipFileName = fileName;
}
//dlg.InitialDirectory = @"D:\Projects\ApiRouting\ApiRouting\bin\Debug";
//dlg.FileName = @"D:\Projects\ApiRouting\ApiRouting\bin\Debug\Pacificwind.grb";
//dlg.Reset();
}
当用户从文件系统的任何位置选择文件时,我将该文件复制到 D:\Projects\ApiRouting\ApiRouting\Files,然后运行 .exe。所以 GenerateCsvFile 方法总是有 fileName 参数值 D:\Projects\ApiRouting\ApiRouting\Files\xyz.grb。那么为什么当我从 D:\Documents 中选择 grib 文件时我的应用程序在 D:\Documents 生成 .csv 文件,以及为什么当我选择.csv 文件来自 D:\Projects\ApiRouting\ApiRouting\Files ?