惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
博客园 - Franky
F
Fortinet All Blogs
D
Docker
博客园 - 司徒正美
腾讯CDC
Recent Announcements
Recent Announcements
The Cloudflare Blog
B
Blog RSS Feed
GbyAI
GbyAI
T
Tailwind CSS Blog
雷峰网
雷峰网
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志

博客园 - Findekano

C#异步调用的bug? 恩,VS调试时候的监视也不能够尽信啊 最近正在看的几本书 VS.NET2005使用体验(二) [C# FAQ]通过Windows Forms预处理Win32消息 将应用程序直接加入到"Run..."中打开的小工具 VS.NET2005使用体验(一) OpenGL & MFC 相关联接 .::Findekano's Tidbet:: .::Findekano's Tidbet:: .::Findekano's Tidbet:: .:: Findekano's Tidbet :: CorelDraw's Application Recovery Manager.. .:: Findekano's Tidbet :: C#&.NET Framework中的Beep .:: Findekano's Tidbet :: 今天开始,努力攒钱... 又闻到味道 如何保持代码格式?
[C# FAQ]C#代码中如何启动另一个应用程序或批处理程序?
Findekano · 2004-06-05 · via 博客园 - Findekano

original URL: How can I run another application or batch file from my Visual C# .NET code?
Posted by: Duncan Mackenzie, MSDN
This post applies to Visual C# .NET 2002/2003


如果你要运行一个命令行程序,或者打开一个windows应用程序,或者打开默认的web浏览器或email客户端,..你应该如何在你的C#代码中实现这个功能呢?
以下这些例子完成相同的任务,你可以使用System.Diagnostics.Process中的类和方法完成这些任务,甚至作的更多。
例1:不管输出结果,仅仅是运行一个命令行程序:

private void simpleRun_Click(object sender, System.EventArgs e){
 System.Diagnostics.Process.Start(@"C:\listfiles.bat");
}

例2. 得到程序运行结果等待直到程序中止(同步运行程序)private void runSyncAndGetResults_Click(object sender, System.EventArgs e){
 System.Diagnostics.ProcessStartInfo psi =
  
new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");
 psi.RedirectStandardOutput =
true;
 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 psi.UseShellExecute =
false;
 System.Diagnostics.Process listFiles;
 listFiles = System.Diagnostics.Process.Start(psi);
 System.IO.StreamReader myOutput = listFiles.StandardOutput;
 listFiles.WaitForExit(2000);
 
if (listFiles.HasExited)
 {
  
string output = myOutput.ReadToEnd();
  
this.processResults.Text = output;
 }
}

 例3. 使用用户机器里的默认浏览器显示URL
private void launchURL_Click(object sender, System.EventArgs e){
 
string targetURL = @http://www.duncanmackenzie.net;
 System.Diagnostics.Process.Start(targetURL);
}

我的看法是,同样是打开浏览器显示URL,使用例3种的方法比启动IE并以URL作为参数要来得合理。
例3的代码将会启动用户的默认浏览器,而并不总是IE。这样你更有可能给用户带来他们所希望得到的体验,
并且可以利用具有最新连接信息的浏览器。