標籤:style class blog code http tar
最近有一個項目需求,需要調用一個exe,就上網查詢了一下,順利的完成了工作,感覺雖然簡單,但挺有意思,就記錄一下。
一,建立一個進程
1,程式碼檢視(控制台程式)
2,代碼
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace KillProcess{ class Program { static void Main(string[] args) { if (args == null || args.Length == 0) return; if (args[0].Equals("help") || args[0].Equals("?")||args[0].Equals("-help")) { Console.WriteLine(" 使用該進程,可以殺掉進程 命令形式如下:"); Console.WriteLine(" KillProcess [-ProcessName]"); Console.WriteLine(" ProcessName 要殺掉的進程的名稱"); } Process[] ps = null; foreach (String pName in args) { ps = Process.GetProcessesByName(pName); if (ps != null && ps.Length > 0) { foreach (Process p in ps) { p.Kill(); } } } } }}View Code
二,用CMD調用
三,用程式調用
1.程式碼檢視
2.代碼
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace InvokeProcess{ class Program { static void Main(string[] args) { Process process = new Process(); process.StartInfo.WorkingDirectory = @"E:\AA\ProcessTest\KillProcess\bin\Debug\"; process.StartInfo.Arguments = "notepad"; process.StartInfo.FileName = "KillProcess"; process.Start(); process.WaitForExit(); } }}View Code