如何通过C#程序运行外部程序?

ahq*_*ing 27 c#

如何通过C#程序运行记事本或计算器等外部程序?

vit*_*ira 34

也许它会帮助你:

System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = @"C:\Users\Vitor\ConsoleApplication1.exe";
pProcess.StartInfo.Arguments = "olaa"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
Run Code Online (Sandbox Code Playgroud)

  • 请记住处理该进程或在`using(Process pProcess = new Process()){}`块中使用它 (10认同)

Mit*_*eat 26

使用System.Diagnostics.Process.Start

可能的重复:如何从C#(WinForms)启动进程

  • 没有死是因为它不会引导任何地方,死是因为它实际上没有显示示例。 (2认同)

Ram*_*nan 11

嗨,这是调用Notepad.exe的示例控制台应用程序,请查看此处.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Demo_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            Process ExternalProcess = new Process();
            ExternalProcess.StartInfo.FileName = "Notepad.exe";
            ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
            ExternalProcess.Start();
            ExternalProcess.WaitForExit();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Inc*_*ito 8

例如这样:

// run notepad
System.Diagnostics.Process.Start("notepad.exe");

//run calculator
System.Diagnostics.Process.Start("calc.exe");
Run Code Online (Sandbox Code Playgroud)

按照米奇回答中的链接.