从 C# 代码运行 exe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9679375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:24:59  来源:igfitidea点击:

Run an exe from C# code

c#.net

提问by hari

I have an exe file reference in my C# project. How do I invoke that exe from my code?

我的 C# 项目中有一个 exe 文件引用。如何从我的代码中调用该 exe?

采纳答案by Logan B. Lehman

using System.Diagnostics;

class Program
{
    static void Main()
    {
        Process.Start("C:\");
    }
}

If your application needs cmd arguments, use something like this:

如果您的应用程序需要 cmd 参数,请使用以下内容:

using System.Diagnostics;

class Program
{
    static void Main()
    {
        LaunchCommandLineApp();
    }

    /// <summary>
    /// Launch the application with some options set.
    /// </summary>
    static void LaunchCommandLineApp()
    {
        // For the example
        const string ex1 = "C:\";
        const string ex2 = "C:\Dir";

        // Use ProcessStartInfo class
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "dcm2jpg.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

        try
        {
            // Start the process with the info we specified.
            // Call WaitForExit and then the using statement will close.
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }
        catch
        {
             // Log error.
        }
    }
}

回答by miksiii

Example:

例子:

System.Diagnostics.Process.Start("mspaint.exe");

Compiling the Code

编译代码

Copy the code and paste it into the Mainmethod of a console application. Replace "mspaint.exe" with the path to the application you want to run.

复制代码并将其粘贴到控制台应用程序的Main方法中。将“mspaint.exe”替换为您要运行的应用程序的路径。

回答by Hamid

Example:

例子:

Process process = Process.Start(@"Data\myApp.exe");
int id = process.Id;
Process tempProc = Process.GetProcessById(id);
this.Visible = false;
tempProc.WaitForExit();
this.Visible = true;

回答by twitchax

I know this is well answered, but if you're interested, I wrote a library that makes executing commands much easier.

我知道这是一个很好的答案,但是如果您有兴趣,我编写了一个库,可以更轻松地执行命令。

Check it out here: https://github.com/twitchax/Sheller.

在这里查看:https: //github.com/twitchax/Sheller