c#关闭系统进程以及如何调用cmd并执行命令

http://www.sina.com.cn 2008年06月20日 10:14  IT168.com


  【IT168技术文档】

  今天翻到了以前很早很早之前写过的用来学习C#的一个小程序,里面有关闭进程的一个小例子,使用了两种方法,一种是调用cmd执行强制关闭命令,另一种是通过C#提供的方法进行关闭

   先说第一种

string ProcessName="explorer";//这里换成你需要删除的进程名称
                Process[] MyProcess1=Process.
GetProcessesByName(ProcessName); Process MyProcess
=new Process(); //设定程序名 MyProcess.StartInfo.FileName="cmd.exe"; //关闭Shell的使用 MyProcess.StartInfo.UseShellExecute=false; //重定向标准输入 MyProcess.StartInfo.RedirectStandardInput=true; //重定向标准输出 MyProcess.StartInfo.RedirectStandardOutput=true; //重定向错误输出 MyProcess.StartInfo.RedirectStandardError=true; //设置不显示窗口 MyProcess.StartInfo.CreateNoWindow=true; //执行强制结束命令 MyProcess.Start(); MyProcess.StandardInput.WriteLine("ntsd -c q -p "
+(MyProcess1[0].Id).ToString());//直接结束进程ID MyProcess.StandardInput.WriteLine("Exit"); 第二种,通过强大的进程类进行标准关闭。 string ProcessName="explorer";//换成想要结束的进程名字 Process[] MyProcess=Process.
GetProcessesByName(ProcessName); MyProcess[
0].Kill();

发表评论 _COUNT_条
Powered By Google
不支持Flash
·城市对话改革30年 ·新浪城市同心联动 ·诚招合作伙伴 ·企业邮箱畅通无阻
不支持Flash