【问题标题】:How can I run an EXE file from my C# code?如何从我的 C# 代码运行 EXE 文件?
【发布时间】:2012-03-29 13:53:40
【问题描述】:

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

【问题讨论】:

    标签: c# .net


    【解决方案1】:
    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.
            }
        }
    }
    

    【讨论】:

    • startInfo.UseShellExecute = false 是一件很棒的事情......它对我来说就像一个魅力!谢谢! :)
    • @logganB.lehman 进程在 exeProcess.WaitForExit() 上永远挂起;有什么想法吗?
    【解决方案2】:

    【讨论】:

    • 你的答案可以更全面吗?和/或指向重复项?
    【解决方案3】:

    例子:

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

    编译代码

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

    【讨论】:

    • 这如何提供比已经创建的答案更多的价值?接受的答案还显示了Process.Start() 的用法
    • SO - 可以通过简化的、逐步的示例来帮助初学者,其中删除了许多细节。也可以使用大写:P
    • 我只需要一种快速的方法来执行 exe,这真的很有帮助。谢谢你:)
    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      相关资源
      最近更新 更多