C#实现强制关闭当前程序进程
时间:2022-04-15 09:30:13|栏目:.NET代码|点击: 次
/// <summary>
/// 运行DOS命令
/// DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
public static string RunCmd(string command)
{
//??例一??Process?,??右????立?M程
System.Diagnostics.Process p = new System.Diagnostics.Process();
//Process?有一??StartInfo?傩裕??@??是ProcessStartInfo?,包括了一些?傩院头椒ǎ?下面我??用到了他的????傩裕?
p.StartInfo.FileName = "cmd.exe"; //?O定程序名
p.StartInfo.Arguments = "/c " + command; //?O定程式?绦???
p.StartInfo.UseShellExecute = false; //?P?]Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向??瘦?入
p.StartInfo.RedirectStandardOutput = true; //重定向??瘦?出
p.StartInfo.RedirectStandardError = true; //重定向?e?`?出
p.StartInfo.CreateNoWindow = true; //?O置不?@示窗口
p.Start(); //???
//p.StandardInput.WriteLine(command); //也可以用?@?N方式?入要?绦械拿?令
//p.StandardInput.WriteLine("exit"); //不?^要?得加上Exit要不然下一行程式?绦械?r候?????C
return p.StandardOutput.ReadToEnd(); //?妮?出流取得命令?绦薪Y果
}
在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 ");
}
}
上一篇:ASP.NET DropDownList控件的使用方法
栏 目:.NET代码
本文标题:C#实现强制关闭当前程序进程
本文地址:http://www.codeinn.net/misctech/199199.html






