Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run .exe from C#

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 ?

like image 224
Shailesh Jaiswal Avatar asked Nov 30 '25 02:11

Shailesh Jaiswal


2 Answers

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")
like image 87
Oded Avatar answered Dec 02 '25 14:12

Oded


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.

like image 43
Steve Avatar answered Dec 02 '25 14:12

Steve