2

I am developing wpf application in C#. I am using grib files in my application. I want to convert this grib file content into csv file. From command prompt I can easily do this. For this in command prompt I need to go to the folder location of degrib.exe i.e. c:\ndfd\degrib\bin. For any other path command will not get executed. I am using the following commands

C:\ndfd\degrib\bin\degrib D:\Documents\Pacificwind.grb -C -msg 1 -Csv
C:\ndfd\degrib\bin\degrib D:\Documents\Pacificwind.grb -C -msg all -nMet -Csv

The commands get successfully executed. I am able to see generated csv files at C:\ndfd\degrib\bin folder. How should I execute the same command from C#. I have seen different examples but none of them worked for me. Can you please provide me any code or link through which I can resolve the above issue ?

Shailesh Jaiswal
  • 3,606
  • 13
  • 73
  • 124

5 Answers5

9

This will work, unless the paths you provided are incorrect:

Process.Start(@"C:\ndfd\degrib\bin\degrib", 
              @"D:\Documents\Pacificwind.grb -C -msg 1 -Csv");

Process.Start(@"C:\ndfd\degrib\bin\degrib", 
              @"D:\Documents\Pacificwind.grb -C -msg all -nMet -Csv")
Oded
  • 489,969
  • 99
  • 883
  • 1,009
3

You could use the ProcessStartInfo class to set the working directory for the application launched.
For example

        ProcessStartInfo pInfo = new ProcessStartInfo("degrib.exe");
        pInfo.WorkingDirectory = @"C:\ndfd\degrib\bin" 
        pInfo.Arguments = @"D:\Documents\Pacificwind.grb -C -msg 1 -Csv";    
        Process p = Process.Start(pInfo);

        // Are I assume that the second processing need to wait for the first to finish
        p.WaitForExit();

        // Start the second run.....
        pInfo = new ProcessStartInfo("degrib.exe");
        pInfo.WorkingDirectory = @"C:\ndfd\degrib\bin" 
        pInfo.Arguments = @"D:\Documents\Pacificwind.grb -C -msg all -nMet -Csv";    
        Process.Start(pInfo);

Check also the documentation on Process class and the WaitForExit method

EDIT: I really do not know what it was degrib, now I have updated the answer to a reasonable assumption of what you're trying to get. Please let me know if paths and executable name are correct.

Steve
  • 213,761
  • 22
  • 232
  • 286
0

You can use following method to execute your exe file

System.Diagnostics.Process.Start(exePath + "LSAPP.exe");
Sanjay Gupta
  • 186
  • 2
  • 11
0
using (Process process = Process.Start(...))
    process.WaitForExit(); // You can wait for process to exit or go idle.
AgentFire
  • 8,944
  • 8
  • 43
  • 90
0

You could use the Process.Start() Create an Process object like so process = new Process { StartInfo = startInfo }; and create your ProcessStartInfo object

startInfo = new ProcessStartInfo(pathToExecutable, arguments);
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
startInfo.RedirectStandardOutput = true;
process = new Process { StartInfo = startInfo };

And catch your output using process.OutputDataReceived event

Jordy van Eijk
  • 2,718
  • 2
  • 19
  • 37