我如何选择一个应用程序以在 c# 中启动进程控件?

问题描述

我一直在查看其他帖子以了解如何在 c# 中启动应用程序。我正在制作一个应用程序启动器,并添加了过程控制。我写了这段代码

Process.FileName = "myapp.exe";
Process.Start();

我认为这不是确切的代码,但第 1 行是我遇到错误的地方。错误是即使文件在同一目录下也找不到。

我的ide是visual studio 2019,我使用.net framework 4.8和.net core 5

解决方法

看来您需要改用 ProcessStartInfo 类:

using (Process myProcess = new Process())
{
    myProcess.StartInfo.UseShellExecute = false;   
    myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
    myProcess.StartInfo.CreateNoWindow = true;
    myProcess.Start();
    // This code assumes the process you are starting will terminate itself.
    // Given that is is started without a window so you cannot terminate it
    // on the desktop,it must terminate itself or you can do it programmatically
    // from this application using the Kill method.
}