关闭系统进程,以及如何调用CMD并执行命令

学习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();

下载地址:(很早之前学习C#时写的,方法很笨,不要笑话哦。)
http://files.cnblogs.com/mgod/WindowsINI.rar

你可能感兴趣的:(cmd)