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}