我希望在Windows窗体的加载事件中运行一个命令行应用程序(例如app.exe)。
我已经尝试使用System.Diagnostics.Process.Start(),但是它打开app.exe后立即将其关闭了。
有没有办法可以运行app.exe并让它保持打开状态?
我希望在Windows窗体的加载事件中运行一个命令行应用程序(例如app.exe)。
我已经尝试使用System.Diagnostics.Process.Start(),但是它打开app.exe后立即将其关闭了。
有没有办法可以运行app.exe并让它保持打开状态?
如果你只想让控制台窗口保持打开状态,可以使用以下命令来运行它:
System.Diagnostics.Process.Start( @"cmd.exe", @"/k c:\path\my.exe" );
尝试执行以下操作:
string cmdexePath = @"C:\Windows\System32\cmd.exe";
//notice the quotes around the below string...
string myApplication = "\"C:\\Windows\\System32\\ftp.exe\"";
//the /K keeps the CMD window open - even if your windows app closes
string cmdArguments = String.Format("/K {0}", myApplication);
ProcessStartInfo psi = new ProcessStartInfo(cmdexePath, cmdArguments);
Process p = new Process();
p.StartInfo = psi;
p.Start();
Console.ReadKey();
。private startApp()
{
string command = " -h"; //common help flag for console apps
System.Diagnostics.Process pRun;
pRun = new System.Diagnostics.Process();
pRun.EnableRaisingEvents = true;
pRun.Exited += new EventHandler(pRun_Exited);
pRun.StartInfo.FileName = "app.exe";
pRun.StartInfo.Arguments = command;
pRun.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
pRun.Start();
pRun.WaitForExit();
}
private void pRun_Exited(object sender, EventArgs e)
{
//Do Something Here
}
Console.In.Read()
使其等待按键即可。假设app.exe也是一个C#应用程序,您可以通过Console.ReadLine()
结束它,前提是您控制源代码。
根据您的主/从应用程序设置,您可能有以下两个问题之一:
对于第一个问题,您需要等待/阻塞进程完成(即使用 Process.WaitForExit()
)。
对于第二个问题,似乎从应用程序已经完成了它需要做的事情(或抛出了异常)并立即关闭。尝试从命令提示符中使用相同的参数运行它并检查输出。
app.exe
pause
现在让你的表单指向那个批处理文件。
Console.ReadLine();
- 使用此代码片段等待直到按下回车键
Console.ReadKey();
- 使用此代码等待按下任意键