从 C# 启动应用程序 (.EXE)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/240171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launching an application (.EXE) from C#?
提问by rudigrobler
How can I launch an application using C#?
如何使用 C# 启动应用程序?
Requirements: Must work on Windows XPand Windows Vista.
要求:必须在Windows XP和Windows Vista 上工作。
I have seen a sample from DinnerNow.net sampler that only works in Windows Vista.
我看过 DinnerNow.net 采样器中的一个示例,该示例仅适用于 Windows Vista。
采纳答案by Igal Tabachnik
Use System.Diagnostics.Process.Start()
method.
使用System.Diagnostics.Process.Start()
方法。
Check out this articleon how to use it.
查看这篇文章,了解如何使用它。
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");
回答by Mark S. Rasmussen
System.Diagnostics.Process.Start("PathToExe.exe");
回答by Brian Schmitt
Additionally you will want to use the Environment Variables for your paths if at all possible: http://en.wikipedia.org/wiki/Environment_variable#Default_Values_on_Microsoft_Windows
此外,如果可能,您将希望为您的路径使用环境变量:http: //en.wikipedia.org/wiki/Environment_variable#Default_Values_on_Microsoft_Windows
E.G.
例如
- %WINDIR% = Windows Directory
- %APPDATA% = Application Data - Varies alot between Vista and XP.
- %WINDIR% = Windows 目录
- %APPDATA% = 应用程序数据 - Vista 和 XP 之间差异很大。
There are many more check out the link for a longer list.
还有更多请查看链接以获取更长的列表。
回答by sfuqua
Here's a snippet of helpful code:
这是有用的代码片段:
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;
}
There is much more you can do with these objects, you should read the documentation: ProcessStartInfo, Process.
你可以用这些对象做更多的事情,你应该阅读文档:ProcessStartInfo,Process。
回答by Adam Kane
System.Diagnostics.Process.Start( @"C:\Windows\System32\Notepad.exe" );
回答by NDB
If you have problems using System.Diagnostics like I had, use the following simple code that will work without it:
如果您在使用 System.Diagnostics 时遇到问题,请使用以下简单代码,无需它即可工作:
Process notePad = new Process();
notePad.StartInfo.FileName = "notepad.exe";
notePad.StartInfo.Arguments = "mytextfile.txt";
notePad.Start();
回答by Deadlock
Use Process.Startto start a process.
使用Process.Start启动进程。
using System.Diagnostics;
class Program
{
static void Main()
{
//
// your code
//
Process.Start("C:\process.exe");
}
}
回答by Amin Mohamed
Just put your file.exe in the \bin\Debug folder and use:
只需将您的 file.exe 放在 \bin\Debug 文件夹中并使用:
Process.Start("File.exe");
回答by user6436606
Try this:
尝试这个:
Process.Start("Location Of File.exe");
(Make sure you use the System.Diagnostics library)
(确保您使用 System.Diagnostics 库)