C#实现查杀本地与远程进程的方法
本文实例讲述了C#实现查杀本地与远程进程的方法。分享给大家供大家参考。
具体实现方法如下:
usingSystem; usingSystem.Management; usingSystem.Threading; classxkill { publicstaticvoidusage() { Console.WriteLine(); Console.WriteLine("xkillv1.0Beta,ByR&S"); Console.WriteLine("E-mail:yrwithsh@vip.sina.com"); Console.WriteLine("H-Page:Http://fz5fz.yeah.net"); Console.WriteLine(); Console.WriteLine("Usage: xkill[ip][account][password][programme][time(s)]"); Console.WriteLine("Ifitsthelocalhost,dontneedaccountandpassword"); Console.WriteLine("Example:xkill218.194.X.Xadministrator123qq.exe60"); Console.WriteLine(" xkill218.194.X.XadministratorNULLqq.exe60"); Console.WriteLine(" xkill127.0.0.1qq.exe60(forlocalhost)"); } publicstaticvoidMain(string[]args) { if((args.Length!=5)&&(args.Length!=3)) { usage(); Environment.Exit(0); } while((args[0]=="127.0.0.1")││(args[0]=="192.168.0.1")) { stringKillName=args[1]; ManagementObjectSearcherlink=newManagementObjectSearcher("SELECT*FromWin32_Process"); ManagementObjectCollectionCollection=link.Get(); foreach(ManagementObjectProcessinCollection) { Console.WriteLine("{0}{1}",Convert.ToString(Process["Name"]).PadRight(15),Process["ExecutablePath"]); if(Convert.ToString(Process["Name"])==KillName) { string[]Tparas={"1"}; Process.InvokeMethod("Terminate",Tparas); } } Console.WriteLine(); Console.WriteLine("Terminateityet"); Thread.Sleep(Convert.ToInt32(args[2])*1000); } try{ do { stringKillName=args[3]; ConnectionOptionsConn=newConnectionOptions(); Conn.Username=args[1]; if(args[2]=="NULL") { Conn.Password=""; } elseConn.Password=args[2]; ManagementPathp=newManagementPath("\\"+args[0]+"\root\cimv2"); ManagementScopems=newManagementScope(p,Conn); ObjectQueryoq=newObjectQuery("SELECT*FROMWin32_Process"); ManagementObjectSearcherlink=newManagementObjectSearcher(ms,oq); ManagementObjectCollectionCollection=link.Get(); Console.WriteLine("--------------------------------------------------------"); foreach(ManagementObjectProcessinCollection) { Console.WriteLine("{0}{1}",Convert.ToString(Process["Name"]).PadRight(15),Process["ExecutablePath"]); if(Convert.ToString(Process["Name"])==KillName) { string[]Tparas={"1"}; Process.InvokeMethod("Terminate",Tparas); } } Thread.Sleep(Convert.ToInt32(args[4])*1000); }while(true); } catch(ExceptionexObj) { Console.WriteLine(); Console.WriteLine("failed!"); Console.WriteLine(exObj.Message); } } }
希望本文所述对大家的C#程序设计有所帮助。