C# 中启动进程的三种方法
2024-07-21 02:28:58
供稿:网友
1.启动子进程,不等待子进程结束
private void simplerun_click(object sender, system.eventargs e)
{ system.diagnostics.process.start(@"c:/listfiles.bat");
}
2.启动子进程,等待子进程结束,并获得输出
1private void runsyncandgetresults_click(object sender, system.eventargs e)
2{
3 system.diagnostics.processstartinfo psi = new system.diagnostics.processstartinfo(@"c:/listfiles.bat");
4 psi.redirectstandardoutput = true;
5 psi.windowstyle = system.diagnostics.processwindowstyle.hidden;
6 psi.useshellexecute = false;
7 system.diagnostics.process listfiles;
8 listfiles = system.diagnostics.process.start(psi);
9 system.io.streamreader myoutput = listfiles.standardoutput;
10 listfiles.waitforexit(2000);
11
12 if (listfiles.hasexited)
13 {
14 string output = myoutput.readtoend();
15 this.processresults.text = output;
16 }
17}
183.使用默认的浏览器打开url
1private void launchurl_click(object sender, system.eventargs e)
2{
3 string targeturl = @http://www.duncanmackenzie.net;
4 system.diagnostics.process.start(targeturl);
5}