如何在C#中指定.exe的输出目录

时间:2012-10-12 11:36:08

标签: c# wpf openfiledialog process.start

我正在用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中选择文件并单击提交按钮,然后在D:\ Projects \ ApiRouting \ ApiRouting \ Files中生成.csv文件,这是正确的。当我从D:\ Documents中选择文件并单击提交按钮时,会在D:\ Documents中生成.csv文件。我运行.exe的代码如下

 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中生成.csv文件时从D:\ Documents中选择grib文件以及为什么它在D:\ Projects \ ApiRouting \ ApiRouting \ Files时生成.csv文件当我选择来自D:\ Projects \ ApiRouting \ ApiRouting \ Files?

的.csv文件

1 个答案:

答案 0 :(得分:2)

degrib.exe似乎正在将其输出写入工作目录。你的选择是

  1. 弄清楚degrib是否存在允许您指定输出CSV文件最终位置的参数。
  2. WorkingDirectory的{​​{1}}设置为您要写入的目录。您可以使用以下命令执行此操作:startInfo这应确保将文件写入与GRB文件相同的目录。