【问题标题】:Launching an application (.EXE) from C#?从 C# 启动应用程序 (.EXE)?
【发布时间】:2010-09-19 09:41:20
【问题描述】:

如何使用 C# 启动应用程序?

要求: 必须在 Windows XPWindows Vista 上工作。

我看到了来自 DinnerNow.net 采样器的示例,该示例仅适用于 Windows Vista。

【问题讨论】:

    标签: c# .net windows-vista windows-xp


    【解决方案1】:

    如果您像我一样在使用 System.Diagnostics 时遇到问题,请使用以下简单代码,无需它也能正常工作:

    using System.Diagnostics;
    
    Process notePad = new Process();
    notePad.StartInfo.FileName   = "notepad.exe";
    notePad.StartInfo.Arguments = "mytextfile.txt";
    notePad.Start();
    

    【讨论】:

    • “没有 System.Diagonostics”的情况如何? Process 在 System.Diagnostics 中。
    【解决方案2】:

    使用System.Diagnostics.Process.Start() 方法。

    查看this article 了解如何使用它。

    Process.Start("notepad", "readme.txt");
    
    string winpath = Environment.GetEnvironmentVariable("windir");
    string path = System.IO.Path.GetDirectoryName(
                  System.Windows.Forms.Application.ExecutablePath);
    
    Process.Start(winpath + @"\Microsoft.NET\Framework\v1.0.3705\Installutil.exe",
    path + "\\MyService.exe");
    

    【讨论】:

      【解决方案3】:

      只需将您的 file.exe 放入 \bin\Debug 文件夹并使用:

      Process.Start("File.exe");
      

      【讨论】:

      • 你的答案比之前的所有答案都有哪些改进?
      • 大多数来看这篇文章的人对通常放在调试文件夹中的文件路径感到困惑,所以当他们使用我的提示“File.exe”时,直接理解不需要路径案例。
      【解决方案4】:

      试试这个:

      Process.Start("Location Of File.exe");
      

      (确保使用 System.Diagnostics 库)

      【讨论】:

        【解决方案5】:

        使用 Process.Start 启动进程。

        using System.Diagnostics;
        class Program
        {
            static void Main()
            {
            //
            // your code
            //
            Process.Start("C:\\process.exe");
            }
        } 
        

        【讨论】:

          【解决方案6】:

          这是一个有用的代码:

          using System.Diagnostics;
          
          // Prepare the process to run
          ProcessStartInfo start = new ProcessStartInfo();
          // Enter in the command line arguments, everything you would enter after the executable name itself
          start.Arguments = arguments; 
          // Enter the executable to run, including the complete path
          start.FileName = ExeName;
          // Do you want to show a console window?
          start.WindowStyle = ProcessWindowStyle.Hidden;
          start.CreateNoWindow = true;
          int exitCode;
          
          
          // Run the external process & wait for it to finish
          using (Process proc = Process.Start(start))
          {
               proc.WaitForExit();
          
               // Retrieve the app's exit code
               exitCode = proc.ExitCode;
          }
          

          您可以使用这些对象做更多事情,您应该阅读文档:ProcessStartInfoProcess

          【讨论】:

          • 只是想指出这似乎也适用于 .exes 以外的其他文件类型。只需指向您要打开的文件,Windows 就会尽力打开它:System.Diagnostics.Process.Start(@"C:\Users\Blank\Desktop\PdfFile.pdf");
          • WindowStyle = ProcessWindowStyle.Hidden 用于非 GUI。我第一次运行它时没有 UseShellExecute = false,但它现在可以工作了。不知道那里发生了什么......
          • 如果我不知道exe的全名怎么办,我想调用“PathTo*.exe”这可能吗?我可以在名称的其余部分使用“*”吗?
          • @vishal,此过程用于调用特定的可执行文件。您当然可以尝试使用PathTo*.exe,但我不希望它起作用。 (a) 如果有多个匹配项怎么办? (b) 我希望微软的代码不会允许这样做,因为它的安全性很弱。
          【解决方案7】:
          System.Diagnostics.Process.Start( @"C:\Windows\System32\Notepad.exe" );
          

          【讨论】:

            【解决方案8】:

            此外,如果可能的话,您将希望为您的路径使用环境变量:http://en.wikipedia.org/wiki/Environment_variable#Default_Values_on_Microsoft_Windows

            例如

            • %WINDIR% = Windows 目录
            • %APPDATA% = 应用程序数据 - Vista 和 XP 之间的差异很大。

            还有更多,请查看链接以获得更长的列表。

            【讨论】:

              【解决方案9】:
              System.Diagnostics.Process.Start("PathToExe.exe");
              

              【讨论】:

              • 不知道exe全名怎么办,想调用“PathTo*.exe”这样可以吗?
              • @vishal 你需要编写一个搜索过程来找到可执行文件
              猜你喜欢
              • 2014-08-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-01
              • 1970-01-01
              相关资源
              最近更新 更多