Search code examples
c#wpfopenfiledialogprocess.start

How to specify output directory for .exe in C#


I am developing wpf application in C#. I have one button on which I am browsing file system through Microsoft.Win32.OpenFileDialog. There is one submit button on which I am calling the Process.Start() to run .exe on grib file. The exe generate the .csv files for me successfully. First I browse the file system, select the file and then I click on submit button. My application execution path is D:\Projects\ApiRouting\ApiRouting\bin\Debug. There is one folder in my application at D:\Projects\ApiRouting\ApiRouting\Files. When I select the file from path D:\Projects\ApiRouting\ApiRouting\Files and click on submit button then the .csv files get generated at D:\Projects\ApiRouting\ApiRouting\Files which is correct. When I select the file from D:\Documents and click on submit button the .csv files are generated at D:\Documents. My code to run .exe is as follows

 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();
        }

When the user selects the file from any location of file system I copy that file at D:\Projects\ApiRouting\ApiRouting\Files and then run the .exe. So GenerateCsvFile method always has the fileName parameter value D:\Projects\ApiRouting\ApiRouting\Files\xyz.grb. So why my application is generating the .csv file at D:\Documents when I select the grib file from D:\Documents and why it is generating the .csv file at D:\Projects\ApiRouting\ApiRouting\Files when I select the .csv file from D:\Projects\ApiRouting\ApiRouting\Files ?


Solution

  • degrib.exe seems to be writing its output to whatever the working directory is. Your options are

    1. Figure out if there is an argument to degrib that allows you to specify where the output CSV files should end up.
    2. Set the WorkingDirectory of the startInfo to the directory you would like them written to. You could do this with: startInfo.WorkingDirectory = new FileInfo(fileName).DirectoryName; This should ensure that the files are written to the same directory as the GRB file.