【问题标题】:How to run external program via a C# program?如何通过 C# 程序运行外部程序?
【发布时间】:2010-07-04 05:04:31
【问题描述】:

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

【问题讨论】:

  • 欢迎来到 Stack Overflow。我认为可以肯定地说英语是你的第二语言。为了增加您获得答案的机会,我会将问题标题改写为“如何从 C# 程序打开外部程序?”。还是控制台应用程序、Winforms、Web(希望不是)?提供更多信息,并确保查看 Stack Overflow 常见问题解答。
  • @Michael 我认为 hw 只是如何。

标签: c#


【解决方案1】:

也许对你有帮助:

using(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();
}

【讨论】:

  • 记得处置进程或在using(Process pProcess = new Process()) { }块中使用它
【解决方案2】:

【讨论】:

  • 仅供参考“示例”是一个死链接。指定链接时总是缺点。
  • 那可能只是我的视力下降
  • 没有死的意思是它不会导致任何地方,死的意思是它实际上并没有展示一个例子。
【解决方案3】:

您好,这是调用 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();
        }
    }
}

【讨论】:

    【解决方案4】:

    例如这样:

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

    点击 Mitchs 答案中的链接。

    【讨论】:

      猜你喜欢
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多