您的位置:首页 > 编程语言 > C#

C#中如何启动另一个应用程序或批处理程序

2010-07-30 09:45 260 查看
如果你要运行一个命令行程序,或者打开一个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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐