在程序設計中,我們經常會遇到要從當前的程序跳到另一個程序的設計需求。也就是當前進程創建另一個進程。C#提供了Process使得我們很方便的實現。
1、Process基本屬性和方法
Id //進程的Id
ProcessName //進程的名稱
PriorityClass //進程的優先級
HandleCount //進程句柄數
PrivateMemorySize64 //關聯的進程分配的專用內存量
WorkingSet64 //工作集,關聯的進程分配的物理內存量
StartInfo //進程啟動的相關屬性
.
.
GetProcesses() //獲取當前系統的所有進程
Start() //啟動進程
Kill(); //強行殺死進程
.
.
例如:通過Process.GetProcesses()方法可以獲取當前系統中的進程,再獲取他們的相關屬性值,我們可以就可以實先類似於任務管理中的進程管理了。
2、利用Process調用命令行--cmd
1 /// <summary> 2 /// 執行Cmd 3 /// </summary> 4 /// <param name="argument">cmd命令</param> 5 /// <param name="msg">返回信息</param> 6 /// <param name="directoryPath">路徑</param> 7 /// <param name="closed">是否關閉</param> 8 public static void RunCmd(string argument,out string msg, string directoryPath = "",bool redirect=false) 9 { 10 msg = string.Empty; 11 Process process = new Process(); 12 ProcessStartInfo startInfo=new ProcessStartInfo(); 13 startInfo.FileName = "cmd.exe"; 14 startInfo.Arguments=redirect?@"/c "+argument:@"/k "+argument; 15 startInfo.UseShellExecute=false; //是否需要啟動windows shell 16 startInfo.CreateNoWindow=false; 17 startInfo.RedirectStandardError=redirect; //是否重定向錯誤 18 startInfo.RedirectStandardInput = redirect; //是否重定向輸入 是則不能在cmd命令行中輸入 19 startInfo.RedirectStandardOutput = redirect; //是否重定向輸出,是則不會在cmd命令行中輸出 20 startInfo.WorkingDirectory=directoryPath; //指定當前命令所在文件位置, 21 process.StartInfo = startInfo; 22 process.Start(); 23 if (redirect) 24 { 25 process.StandardInput.Close(); 26 msg = process.StandardOutput.ReadToEnd(); //在重定向輸出時才能獲取 27 } 28 //else 29 //{ 30 // process.WaitForExit();//等待進程退出 31 //} 32 }
這里需要注意三點,1、是重定向,重定向為true時方可獲取返回值,並且要求命令行退出。2、WorkingDirectory,在調用命令行時需注意指定工作目錄,默認都是當前程序啟動目錄。(特別是那些需要在指定目錄運行的注冊exe,當然他算exe范疇了,但會彈出命令行顯示進度,所以再此提一下)3、當程序為控制台時,系統為默認在控制台上執行命令行命令。
3、啟動exe
1 /// <summary> 2 /// 啟動exe 3 /// </summary> 4 /// <param name="filePath">程序路徑</param> 5 /// <param name="argument">參數</param> 6 /// <param name="waitTime">等待時間,毫秒計</param> 7 public void RunExe(string filePath,string argument,int waitTime=0) 8 { 9 if (string.IsNullOrEmpty(filePath)) 10 { 11 throw new Exception("filePath is empty"); 12 } 13 if (!File.Exists(filePath)) 14 { 15 throw new Exception(filePath + " is not exist"); 16 } 17 string directory = Path.GetDirectoryName(filePath); 18 19 try 20 { 21 Process p = new Process(); 22 p.StartInfo.FileName = filePath; 23 p.StartInfo.WorkingDirectory = directory; 24 p.StartInfo.Arguments = argument; 25 p.StartInfo.ErrorDialog = false; 26 //p.StartInfo.UseShellExecute = false; 27 p.StartInfo.CreateNoWindow = true; 28 p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//與CreateNoWindow聯合使用可以隱藏進程運行的窗體 29 p.StartInfo.RedirectStandardOutput = true; 30 p.StartInfo.RedirectStandardInput = true; 31 p.StartInfo.RedirectStandardError = true; 32 p.EnableRaisingEvents = true; // 啟用Exited事件 33 p.Exited+=p_Exited; 34 p.Start(); 35 if (waitTime > 0) 36 { 37 p.WaitForExit(waitTime); 38 } 39 40 if (p.ExitCode == 0)//正常退出 41 { 42 //TODO記錄日志 43 System.Console.WriteLine("執行完畢!"); 44 } 45 } 46 catch (Exception ex) 47 { 48 throw new Exception("系統錯誤:", ex); 49 } 50 51 }
這里要求指定執行文件的絕對地址,當需要執行系統環境配置好的執行文件時就不好了,還要知道他的詳細路徑。如若這樣,可以考慮下面簡單的方式
1 public void RunSysExe(string filePath, string argument) 2 { 3 Process p = new Process(); 4 5 p.StartInfo.FileName = filePath; // "iexplore.exe"; //IE 6 p.StartInfo.Arguments = argument;// "http://www.baidu.com"; 7 p.Start(); 8 }
最后
就寫這么多了,最近鼠標抽風了,各種單機變雙擊......
