2009-08-27 60 views
4

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

代码行示例更有帮助。但任何东西都受到欢迎

谢谢。

+1

尔加。多少种变体“我如何在C#中执行另一个程序?” SO存在的问题? – 2009-08-27 07:05:28

回答

16

您可以使用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 
} 
+3

为了更好地控制流程的开始方式,您应该查看ProcessStartInfo,它也可以用作Process.Start()的参数。看看这里:http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo_members.aspx – Oliver 2009-08-27 06:33:58

+0

是的代码工程!非常感谢,反应也非常快。 – user147685 2009-08-27 07:12:55