C# 强制关闭当前程序进程(完全Kill掉不留痕迹)

发布时间:2010年06月22日      浏览次数:856 次
函数:
/// <summary>
/// 运行DOS命令
/// DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
public static string RunCmd(string command)
{
//实例一个Process类,启动一个独立进程
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe"; //设定程序名
p.StartInfo.Arguments = "/c " + command; //设定程式执行参数
p.StartInfo.UseShellExecute = false; //关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
p.StartInfo.RedirectStandardError = true; //重定向错误输出
p.StartInfo.CreateNoWindow = true; //设置不显示窗口

p.Start(); //启动
return p.StandardOutput.ReadToEnd(); //返回结果
}
==========================================================
在Program.cs加上如下:
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
//强制关闭进程
string exeName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string[] exeArray = exeName.Split('\');

FunctionClass.RunCmd("taskkill /im " + exeArray[exeArray.Length-1] + " /f ");
}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!