您的位置:首页 > 编程语言 > C#

C# 强制关闭当前程序进程

2011-12-28 10:41 411 查看
函数:

/// <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 ");

}

}

转载http://jishu.hao513.com/html/software/csharp/20100622092849.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: