c#将线程名称cmd.exe更改为xxx.exe

时间:2017-07-15 15:58:46

标签: c# windows batch-file cmd

我想创建一个c#应用程序创建一个批处理,在循环中关闭所有cmd和Taskmanager。它与“taskmgr.exe”完美配合,但是如果我添加它应该关闭所有cmd,它也会自动关闭。有没有办法更改自己的线程名称,所以它不再被称为cmd.exe?

我的代码:

            File.Delete(path);
            using (var tw = new StreamWriter(path, true))
            {
                tw.WriteLine(":ttt");
                tw.WriteLine("taskkill /f /Im taskmgr.exe");
                tw.WriteLine("goto ttt:");
                tw.WriteLine("pause");
                tw.Close();
                File.SetAttributes(path, FileAttributes.Hidden);
            }
            Process.Start(path);

1 个答案:

答案 0 :(得分:1)

我认为这里存在一些误解,没有名为" cmd.exe"的线程名称,只是当你运行你的应用程序时,它会创建一个批处理文件,然后是Process.Start(path);运行一个新的cmd应用程序来在其中运行这个新创建的批处理脚本(因为根据定义,.bat脚本是cmd程序)。

我宁愿使用类似的东西直接杀死你的程序中的任务管理器和cmd:

while(true){
    foreach (Process proc in Process.GetProcessesByName("cmd"))
    {
        proc.Kill();
    }
    foreach (Process proc in Process.GetProcessesByName("taskmgr"))
    {
        proc.Kill();
    }
    // Sleep for 2 seconds, so that the program does not do this too often.
    Thread.Sleep(2000);
}