在c#中执行/打开程序

时间:2009-08-27 06:20:03

标签: c# .net

是否有关于如何在C#中打开或执行某些窗口程序的解决方案/参考?例如,如果我想打开WinZIP或记事本应用程序?

代码行上的示例更有帮助。但欢迎任何事情。

谢谢。

1 个答案:

答案 0 :(得分:17)

您可以使用System.Diagnostics.Process.Start方法。

Process.Start("notepad.exe");

它适用于与默认程序相关联的文件:

Process.Start(@"C:\path\to\file.zip"); 

将使用默认应用程序打开文件。

即使使用URL打开浏览器:

Process.Start("http://stackoverflow.com"); // open with default browser

同意@OliverProcessStartInfo为您提供了对此流程的更多控制权,例如:

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.FileName = "notepad.exe";
startInfo.Arguments = "file.txt";
startInfo.WorkingDirectory = @"C:\path\to";
startInfo.WindowStyle = ProcessWindowStyle.Maximized;

Process process = Process.Start(startInfo);

// Wait 10 seconds for process to finish...
if (process.WaitForExit(10000))
{
     // Process terminated in less than 10 seconds.
}
else
{
     // Timed out
}