Search code examples
c#windowsbatch-filecmd

C# Change thread name cmd.exe to xxx.exe


I've wanted to make an C# Application which creates an batch that closes all cmd's and Taskmanager's in a loop. It works perfectly with "taskmgr.exe" but if I add that it should close all cmd's it closes itself too. Is there a way to change the thread name of itself so it isn't called anymore cmd.exe?

My code:

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

Solution

  • I think there is some misunderstanding here, there is no thread name called "cmd.exe", it's just that when you run your application, it creates a batch file, and then the Process.Start(path); runs a new cmd application to run this newly created batch script inside of it (since by definition, .bat scripts are cmd programs).

    I would rather directly kill the task managers and the cmds from within your program using something like:

    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);
    }