-2

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 ?

Shailesh Jaiswal
  • 3,606
  • 13
  • 73
  • 124
  • your escaping seems "odd"... another point: the code you show doesn't have anything to do with the problem you describe (OpenFileDialog)... what is the value of the parameter `fileName` when the above code is executed (check via Debugger)? – Yahia Oct 12 '12 at 11:41
  • 1
    @Yahia, i think he have problems with understanding how degrib.exe works and where it creates result, not OpenFileDialog problems – Dmitrii Dovgopolyi Oct 12 '12 at 11:43
  • filename always has value D:\Projects\ApiRouting\ApiRouting\Files\xyz.grb – Shailesh Jaiswal Oct 12 '12 at 11:46
  • What if you did this `startInfo1.Arguments = @"" + fileName + "" + " -C -msg all -nMet -Csv > outputfile.csv"`? This should pipe the output from `degrib.exe` into a file of your choice. – nick_w Oct 12 '12 at 11:48
  • @NickW what is outputfile.csv? If it is a path can rewrite this command with with some temporary path ? – Shailesh Jaiswal Oct 12 '12 at 11:53
  • `outputfile.csv` was just to indicate the concept. You could change that line instead to: `startInfo1.Arguments = @"" + fileName + "" + " -C -msg all -nMet -Csv > " + fileName + ".csv"` – nick_w Oct 12 '12 at 11:56
  • Scratch that, I don't think piping the output will help in this case. You could try setting the `WorkingDirectory` of `startInfo`. I think you could set it to `new FileInfo(fileName).DirectoryName`. – nick_w Oct 12 '12 at 12:03

1 Answers1

2

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.
nick_w
  • 14,758
  • 3
  • 51
  • 71
  • 1
    Thanks for your continuous support from last 2 days. Finally you have solved my problem. Now everything is working fine.Thanks buddy – Shailesh Jaiswal Oct 12 '12 at 12:40