问题
I have an application that I run on the command prompt as follows:
C:\some_location> "myapplication.exe" headerfile.h
I want to create a Windows Forms application where the user can specify the location of the executable and also the header file so that the Windows Forms application can do this for him/her, and the user wouldn't have to go to the command line and do it.
How can I do this?
回答1:
You need to use the Process class:
Process.Start(@"C:\some_location\myapplication.exe");
For arguments:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\some_location\myapplication.exe";
startInfo.Arguments = "header.h";
Process.Start(startInfo);
Obviously you can pull these names/arguments from text boxes.
回答2:
You can try with this code:
ProcessStartInfo startInfo = new ProcessStartInfo("yourExecutable.exe");
startInfo.Arguments = "header.h"; // Your arguments
Process.Start(startInfo);
回答3:
See ProcessStartInfo.UseShellExecute. This page will provide you with the full information about the .exe process information.
Another way that I used is:
ProcessStartInfo objProcess = new ProcessStartInfo(@"Yours .exe path");
objProcess.UseShellExecute = false;
objProcess.RedirectStandardOutput = true;
Process.Start(objProcess);
And it's working fine.
来源:https://stackoverflow.com/questions/11635856/running-a-exe-application-from-windows-forms