如何从C#启动进程?

149 .net c# windows process process.start

如何启动流程,例如在用户单击按钮时启动URL?

And*_*age 213

正如Matt Hamilton所建议的那样,对进程进行有限控制的快速方法是在System.Diagnostics.Process类上使用静态Start方法......

using System.Diagnostics;
...
Process.Start("process.exe");
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用Process类的实例.这允许对进程进行更多控制,包括调度,它将运行的窗口类型,以及对我来说最有用的等待进程完成的能力.

using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();// Waits here for the process to exit.
Run Code Online (Sandbox Code Playgroud)

这种方法比我提到的控制更多.

  • 您应该使用using语句或处理该过程以及/sf/ask/1187012431/ (3认同)

Mat*_*ton 24

您可以使用System.Diagnostics.Process.Start方法来启动进程.您甚至可以将URL作为字符串传递,它将启动默认浏览器.


GvS*_*GvS 10

正如Matt所说,使用Process.Start.

您可以传递URL或文档.它们将由注册的应用程序启动.

例:

Process.Start("Test.Txt");
Run Code Online (Sandbox Code Playgroud)

这将启动加载了Text.Txt的Notepad.exe.

  • 如果没有为此类型注册程序,会发生什么? (4认同)
  • @LC `Win32Exception` (0x80004005)“没有应用程序与此操作的指定文件关联” (2认同)

Bla*_*ult 8

我在自己的程序中使用了以下内容.

Process.Start("http://www.google.com/etc/etc/test.txt")
Run Code Online (Sandbox Code Playgroud)

这有点基础,但它确实适合我.


Sim*_*erT 6

var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "/YourSubDirectory/yourprogram.exe");
Process.Start(new ProcessStartInfo(path));
Run Code Online (Sandbox Code Playgroud)


Rav*_*G N 6

class ProcessStart
{
    static void Main(string[] args)
    {
        Process notePad = new Process();

        notePad.StartInfo.FileName   = "notepad.exe";
        notePad.StartInfo.Arguments = "ProcessStart.cs";

        notePad.Start();
    }
}
Run Code Online (Sandbox Code Playgroud)


Fra*_*nov 5

使用Process类.MSDN文档有一个如何使用它的示例.


ali*_*ini 5

您可以使用此语法运行任何应用程序:

System.Diagnostics.Process.Start("Example.exe");
Run Code Online (Sandbox Code Playgroud)

和URL一样.只需在此之间写下您的URL即可().

例:

System.Diagnostics.Process.Start("http://www.google.com");
Run Code Online (Sandbox Code Playgroud)


Moo*_*ing 5

如果在 Windows 上使用

Process process = new Process();
process.StartInfo.FileName = "Test.txt";
process.Start();
Run Code Online (Sandbox Code Playgroud)

适用于 .Net Framework,但对于 Net core 3.1 还需要将 UseShellExecute 设置为 true

Process process = new Process();
process.StartInfo.FileName = "Test.txt";
process.StartInfo.UseShellExecute = true;
process.Start();
Run Code Online (Sandbox Code Playgroud)