从C#启动应用程序(.EXE)?

rud*_*ler 156 .net c# windows-xp windows-vista

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

要求:必须适用于Windows XPWindows Vista.

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

sfu*_*qua 226

这是一段有用的代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 只是想指出,这似乎也适用于除.exes之外的其他文件类型.只需指向要打开的文件,Windows就会尽力打开它:System.Diagnostics.Process.Start(@"C:\ Users\Blank\Desktop\PdfFile.pdf"); (7认同)

Iga*_*nik 164

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

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


Mar*_*sen 56

System.Diagnostics.Process.Start("PathToExe.exe");
Run Code Online (Sandbox Code Playgroud)


Ada*_*ane 17

System.Diagnostics.Process.Start( @"C:\Windows\System32\Notepad.exe" );
Run Code Online (Sandbox Code Playgroud)


NDB*_*NDB 13

如果您在使用System.Diagnostics时遇到问题,请使用以下简单代码,无需使用它:

Process notePad = new Process();
notePad.StartInfo.FileName   = "notepad.exe";
notePad.StartInfo.Arguments = "mytextfile.txt";
notePad.Start();
Run Code Online (Sandbox Code Playgroud)

  • 这是如何"没有System.Diagonostics"的?`Process`在System.Diagnostics中. (9认同)

Bri*_*itt 8

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

例如

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

还有更多的链接可以查看更长的列表.


Ami*_*med 5

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

Process.Start("File.exe");
Run Code Online (Sandbox Code Playgroud)

  • 你的答案比以前的所有答案有什么改进? (4认同)