How to run .exe from C#

拟墨画扇 提交于 2019-12-11 05:19:27

问题


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 ?


回答1:


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")



回答2:


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.




回答3:


You can use following method to execute your exe file

System.Diagnostics.Process.Start(exePath + "LSAPP.exe");



回答4:


using (Process process = Process.Start(...))
    process.WaitForExit(); // You can wait for process to exit or go idle.



回答5:


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



来源:https://stackoverflow.com/questions/12636991/how-to-run-exe-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!