3

I wrote this little function that searches for a process by name and kills it: see code below

Process[] procList = Process.GetProcesses();
RegistryKey expKey = Registry.LocalMachine;
expKey = expKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);
expKey.SetValue("AutoRestartShell", 0);

        using (StreamWriter writer = new StreamWriter(@"C:\test\progtemp\Procs1.Txt", true))

        {

            int i = 0;

            foreach (Process procs in procList)

            {

                writer.WriteLine(string.Format("Process Name {0} -- Process ID {1} -- Session ID {2}", procs.ProcessName, procs.Id, procs.SessionId));

                if (procs.ProcessName == "explorer")

                {

                    procList[i].Kill();

                }

                i++;

            }

        }
expKey.SetValue("AutoRestartShell", 1);

I'm curious why when I tell it to kill explorer it automatically restarts. How can I make it so that it does not restart and you have to go into task manager and manually restart it?

user3038431
  • 113
  • 1
  • 8
  • Explorer.exe is what's used to provide the Windows shell. Microsoft made a decision that if it crashed it made sense to attempt to restart it. – Jeff Prince Aug 22 '15 at 17:48

1 Answers1

6

If you run regedit and go into HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon, you can find there a key named AutoRestartShell.

Settings that to 0 would disallow explorer.exe from rebooting. Although I personally say it's not the best idea to mess with Registry just for that but if you really need to, make use of Registry.SetValue to change that value to 0 from the code (documentation: https://msdn.microsoft.com/en-us/library/5a5t63w8(v=vs.110).aspx)

Edit: inspiration taken from https://technet.microsoft.com/en-us/library/cc939703.aspx

Edit 2: digging a bit into google came up with the following result which explains everything slightly better: https://superuser.com/questions/511914/why-does-explorer-restart-automatically-when-i-kill-it-with-process-kill

Community
  • 1
  • 1
Propolys
  • 364
  • 1
  • 16