182

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

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

我从 DinnerNow.net 采样器中看到了一个仅适用于 Windows Vista 的示例。

4

9 回答 9

236

这是一段有用的代码:

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

于 2008-10-27T16:55:15.890 回答
182

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

查看这篇文章,了解如何使用它。

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");
于 2008-10-27T14:58:40.480 回答
62
System.Diagnostics.Process.Start("PathToExe.exe");
于 2008-10-27T14:58:35.767 回答
21
System.Diagnostics.Process.Start( @"C:\Windows\System32\Notepad.exe" );
于 2009-10-13T06:30:33.100 回答
16

如果您像我一样在使用 System.Diagnostics 时遇到问题,请使用以下简单代码,在没有它的情况下也可以使用:

using System.Diagnostics;

Process notePad = new Process();
notePad.StartInfo.FileName   = "notepad.exe";
notePad.StartInfo.Arguments = "mytextfile.txt";
notePad.Start();
于 2014-06-03T21:23:45.110 回答
8

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

例如

  • %WINDIR% = Windows 目录
  • %APPDATA% = 应用程序数据 - 在 Vista 和 XP 之间变化很大。

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

于 2008-10-27T16:22:52.947 回答
5

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

Process.Start("File.exe");
于 2016-04-15T01:33:33.510 回答
2

使用Process.Start启动一个进程。

using System.Diagnostics;
class Program
{
    static void Main()
    {
    //
    // your code
    //
    Process.Start("C:\\process.exe");
    }
} 
于 2015-08-27T03:38:43.513 回答
1

试试这个:

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

(确保使用 System.Diagnostics 库)

于 2016-06-07T17:35:36.093 回答