當(dāng)前位置:首頁 > IT技術(shù) > Windows編程 > 正文

在c#中調(diào)用另一個應(yīng)用程序或命令行(.exe 帶參數(shù))
2021-08-07 15:59:18

在.net中使用system.diaglostics.Process可以用來調(diào)用另一個命令行或程序。

using???System.Diagnostics;???
??如果是dos???
??Process.Start("cmd.exe");???
??如果是其他文件???
??Process.Start("絕對路徑+文件名.exe");???
??------------------------------------???
??如何在c#中調(diào)用外部dos程序????
??使用Process對象:???????
??System.Diagnostics.Process?????p=new?????System.Diagnostics.Process();???????
??p.StartInfo.FileName="arj.exe"?????;//需要啟動的程序名???????
??p.StartInfo.Arguments="-x?????sourceFile.Arj?????c:/temp";//啟動參數(shù)???????
??p.Start();//啟動???????
??if(p.HasExisted)//判斷是否運(yùn)行結(jié)束???????
????p.kill();???



-------------------------------------------------------------------------------------------------------------------------------------
///???<summary>???
??///???啟動其他的應(yīng)用程序???
??///???</summary>???
??///???<param???name="file">應(yīng)用程序名稱</param>???
??///???<param???name="workdirectory">應(yīng)用程序工作目錄</param>???
??///???<param???name="args">命令行參數(shù)</param>???
??///???<param???name="style">窗口風(fēng)格</param>???
??public???static???bool???StartProcess(string???file,string???workdirectory,string???args,ProcessWindowStyle???style)???
??{???
??try???
??{???
??Process???myprocess???=???new???Process();???
??ProcessStartInfo???startInfo???=???new???ProcessStartInfo(file,args);???
??startInfo.WindowStyle???=???style;???
??startInfo.WorkingDirectory???=???workdirectory;???
??myprocess.StartInfo???=???startInfo;???
??myprocess.StartInfo.UseShellExecute???=???false;???
??myprocess.Start();???
??return???true;???
??}???
??catch(Exception???e0)???
??{???
??MessageBox.Show("啟動應(yīng)用程序時出錯!原因:"???+???e0.Message);???
??}???
??return???false;???
??}???
????
????
????
??string???parms???=???""???+???GlobalObject.GetInstance().UserID???+???"???"???+???GlobalObject.GetInstance().UserPassword;???
??if???(PublicMethods.StartProcess(Application.StartupPath???+???@"/uptool/uptool.exe",Application.StartupPath???+???"http://UpTool",parms,ProcessWindowStyle.Normal))???
??{???
??Environment.Exit(0);???
??}???
----------------------------------------------------------------------------------------------------------------------
Process.Start("IExplore.exe",???"http://www.newhappy.cn");??
System.Diagnostics.ProcessStartInfo???startInfo???=???new???System.Diagnostics.ProcessStartInfo(???);???
??startInfo.FileName???=???"執(zhí)行EXE的文件名";???
??startInfo.Arguments???=???"參數(shù)數(shù)組";???
??System.Diagnostics.Process.Start(???startInfo???);
----------------------------------------------------------------------------------------------------------------------------
1.有好多時,我們需要調(diào)用外部的EXE程序,并且要等它運(yùn)行完畢,我們才可以繼續(xù)下面的動作,那我們怎樣去實(shí)現(xiàn)了,請看以下代碼.
????????'怎樣等待外部程序運(yùn)行完畢.
????????'從系統(tǒng)資料夾讀入文件
????????Dim?sysFolder?As?String?=?_
????????????????????Environment.GetFoldERPath(Environment.SpecialFolder.System)
????????'創(chuàng)建一個新的進(jìn)程結(jié)構(gòu)
????????Dim?pInfo?As?New?ProcessStartInfo()
????????'設(shè)置其成員FileName為系統(tǒng)資料的Eula.txt
????????pInfo.FileName?=?sysFolder?&?"/eula.txt"
????????'運(yùn)行該文件
????????Dim?p?As?Process?=?Process.Start(pInfo)
????????'等待程序裝載完成
????????p.WaitForInputIdle()
????????'等待進(jìn)行程退出
????????p.WaitForExit()
????????'繼續(xù)執(zhí)行下面的代碼
????????MessageBox.Show("繼續(xù)執(zhí)行代碼")


2.我們想在5秒鐘后,強(qiáng)行關(guān)閉它.而不是需要我手工關(guān)閉.
????'設(shè)置退出時間
????Dim?timeOut?As?Integer?=?5000
????Dim?sysFolder?As?String?=?_
?????????Environment.GetFolderPath(Environment.SpecialFolder.System)
????Dim?pInfo?As?New?ProcessStartInfo()
????pInfo.FileName?=?sysFolder?&?"/eula.txt"
????Dim?p?As?Process?=?Process.Start(pInfo)
????p.WaitForInputIdle()
????p.WaitForExit(timeOut)
????'檢查是否在超時前已關(guān)閉了.
????If?p.HasExited?=?False?Then
????????'進(jìn)行程還在運(yùn)行
????????'看進(jìn)程有沒有回應(yīng)
????????If?p.Responding?Then
????????????p.CloseMainWindow()?'關(guān)閉窗口
????????Else
????????????p.Kill()??'強(qiáng)行中斷
????????End?If
????End?If
????MessageBox.Show("繼續(xù)執(zhí)行代碼")

?
?
?
?

本文摘自 :https://blog.51cto.com/u

開通會員,享受整站包年服務(wù)立即開通 >