从C#代码运行exe

时间:2012-03-13 06:35:57

标签: c# .net

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

5 个答案:

答案 0 :(得分:244)

using System.Diagnostics;

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

如果您的应用程序需要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.
        }
    }
}

答案 1 :(得分:16)

答案 2 :(得分:5)

示例:

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

编译代码

复制代码并将其粘贴到控制台应用程序的 Main 方法中。 替换&#34; mspaint.exe&#34;以及要运行的应用程序的路径。

答案 3 :(得分:4)

实施例:

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

答案 4 :(得分:1)

我知道这是很好的答案,但是如果您有兴趣,我写了一个库,使执行命令更加容易。

在此处查看:https://github.com/twitchax/Sheller