wpf 如何从 C# 运行 .exe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12636991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run .exe from C#
提问by Shailesh Jaiswal
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# 开发 wpf 应用程序。我在我的应用程序中使用 grib 文件。我想将此 grib 文件内容转换为 csv 文件。从命令提示符我可以轻松地做到这一点。为此,我需要在命令提示符下转到 degrib.exe 的文件夹位置,即 c:\ndfd\degrib\bin。对于任何其他路径命令将不会被执行。我正在使用以下命令
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 ?
命令成功执行。我可以在 C:\ndfd\degrib\bin 文件夹中看到生成的 csv 文件。我应该如何从 C# 执行相同的命令。我见过不同的例子,但没有一个对我有用。您能否提供任何代码或链接以解决上述问题?
回答by Oded
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")
回答by Steve
You could use the ProcessStartInfoclass to set the working directory for the application launched.
For example
您可以使用ProcessStartInfo类为启动的应用程序设置工作目录。
例如
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 Processclass and the WaitForExit method
另请查看有关Process类和 WaitForExit 方法的文档
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.
编辑:我真的不知道它是什么degrib,现在我已经更新了你想要得到的合理假设的答案。请让我知道路径和可执行文件名称是否正确。
回答by Sanjay Gupta
You can use following method to execute your exe file
您可以使用以下方法来执行您的 exe 文件
System.Diagnostics.Process.Start(exePath + "LSAPP.exe");
回答by AgentFire
using (Process process = Process.Start(...))
process.WaitForExit(); // You can wait for process to exit or go idle.
回答by Jordy van Eijk
You could use the Process.Start()Create an Process object like so
process = new Process { StartInfo = startInfo };
and create your ProcessStartInfo object
您可以Process.Start()像这样使用Create an Process 对象 process = new Process { StartInfo = startInfo }; 并创建您的 ProcessStartInfo 对象
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
并使用
process.OutputDataReceived event

