如图,任务管理器中的命令行
不说别的,上代码
部分using:
using System.Diagnostics; using System.Management;
其中要引用System.Management
1.通过筛选CommandLine查出pid
其中涉及Environment类的一些知识,大家可以自行查看:链接https://msdn.microsoft.com/zh-cn/library/system.environment(v=vs.110).aspx
/// <summary> /// 通过筛选CommandLine查出pid /// </summary> /// <param name="thanName1">条件one</param> /// <returns></returns> private static List<processId1> GetProcessId(string thanName1) { List<processId1> results = new List<processId1>(); SelectQuery query1 = new SelectQuery("Select * from Win32_Process WHERE CommandLine like '%" + thanName1 + "%'"); ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(query1); try { foreach (ManagementObject disk in searcher1.Get()) { results.Add(new processId1() { processId01 = disk["ProcessId"].ToString(), CommandLine = disk["CommandLine"].ToString(), name = disk["Name"].ToString() }); } } catch { return null; } return results; }
2.通过pid杀进程
/// <summary> /// 通过pid杀进程 /// </summary> /// <param name="strProcid"></param> public void KillProcessid(string strProcid) { try { foreach (Process p in Process.GetProcesses()) { if (p.Id.Equals(Int32.Parse(strProcid))) { if (!p.CloseMainWindow()) { p.Kill(); } else { p.Kill(); } } } } catch { MessageBox.Show("Error"); } }
3.button里的
private void btn_skilladvertising_Click(object sender, EventArgs e) { string criteriaOne = "QQ"; string str = null; try { foreach (processId1 item in GetProcessId(criteriaOne)) { KillProcessid(item.processId01); str += "已结束 " + item.processId01 + "\n"; } } catch { str = "Error"; } MessageBox.Show(str); }
4.我构造的类,其中有些没用到,大家自己修改
public class processId1 { public string CommandLine { get; set; } public string processId01 { get; set; } public string name { get; set; } }
5.其他一些结束进程的方法可以参照http://blog.csdn.net/xrongzhen/article/details/5838690
6.其他的一些知识:
(1).this.MaximizeBox = false;//禁止窗口手动最大化,包括双击标题栏
(2).this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;//最开始窗口出现在屏幕中心
2016-11-11 14:28:57