Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Shut down explorer programmatically

Shut down explorer programmatically

Scheduled Pinned Locked Moved C#
csharpc++linuxtutorialquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ian Bowler
    wrote on last edited by
    #1

    I need to shutdown explorer programmatically. I had to do this once in a C++ project and I used the following code: HWND hwndShell = NULL; hwndShell = FindWindow("Progman", NULL); // Locate the Windows shell if(hwndShell != NULL) { PostMessage(hwndShell, WM_QUIT, 0, 0L)// Tell the Windows shell to quit } Any ideas on how to do this in C#? Thanks! Ian

    T 1 Reply Last reply
    0
    • I Ian Bowler

      I need to shutdown explorer programmatically. I had to do this once in a C++ project and I used the following code: HWND hwndShell = NULL; hwndShell = FindWindow("Progman", NULL); // Locate the Windows shell if(hwndShell != NULL) { PostMessage(hwndShell, WM_QUIT, 0, 0L)// Tell the Windows shell to quit } Any ideas on how to do this in C#? Thanks! Ian

      T Offline
      T Offline
      tommazzo
      wrote on last edited by
      #2

      You can do this using the System.Diagnostics namespace. The following code will kill explorer.exe: Process[] RunningProcesses = Process.GetProcessesByName("explorer"); Process proc = new Process(); foreach(Process p in RunningProcesses) { if(p.ProcessName == "explorer") { proc = p; break; } } proc.Kill(); When I tested it however explorer.exe restarted a few seconds after it was killed. This might be due to some program I had running, but I'm not sure.

      I D 2 Replies Last reply
      0
      • T tommazzo

        You can do this using the System.Diagnostics namespace. The following code will kill explorer.exe: Process[] RunningProcesses = Process.GetProcessesByName("explorer"); Process proc = new Process(); foreach(Process p in RunningProcesses) { if(p.ProcessName == "explorer") { proc = p; break; } } proc.Kill(); When I tested it however explorer.exe restarted a few seconds after it was killed. This might be due to some program I had running, but I'm not sure.

        I Offline
        I Offline
        Ian Bowler
        wrote on last edited by
        #3

        Thanks alot! I wonder how you kill it for good? I want to control when it starts up again. Anyone know how to do that? Thanks! Ian

        D 1 Reply Last reply
        0
        • T tommazzo

          You can do this using the System.Diagnostics namespace. The following code will kill explorer.exe: Process[] RunningProcesses = Process.GetProcessesByName("explorer"); Process proc = new Process(); foreach(Process p in RunningProcesses) { if(p.ProcessName == "explorer") { proc = p; break; } } proc.Kill(); When I tested it however explorer.exe restarted a few seconds after it was killed. This might be due to some program I had running, but I'm not sure.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          tommazzo wrote: When I tested it however explorer.exe restarted a few seconds after it was killed. It's supposed to do this. By default, Explorer automatically restarts itself if it's shutdown without a user session logoff. Sometimes, though, it doesn't restart itself. When this happens, you can still hit Ctrl-Alt-Del, start the Task Manager, click on File/New Task (Run), then type CMD in the prompt, NOT EXPLORER! Instead, type CMD and in the resulting command window, type START EXPLORER. For some reason, if Explorer crashes and you type EXPLORER in the Start/Run box, it won't restart. Though, typing the START EXPLORER command in a CMD window works. Wierd... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          1 Reply Last reply
          0
          • I Ian Bowler

            Thanks alot! I wonder how you kill it for good? I want to control when it starts up again. Anyone know how to do that? Thanks! Ian

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            You can't. Explorer automatically restarts itself. But, I seem to, vaguely(!), remember a policy somewhere that controls this. I can't find it and I can't remember where I saw it, though. BTW: Why on earth would you want to do this anyway? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            I 1 Reply Last reply
            0
            • D Dave Kreskowiak

              You can't. Explorer automatically restarts itself. But, I seem to, vaguely(!), remember a policy somewhere that controls this. I can't find it and I can't remember where I saw it, though. BTW: Why on earth would you want to do this anyway? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              I Offline
              I Offline
              Ian Bowler
              wrote on last edited by
              #6

              Why? It's a long story... The C++ code in the original post shuts down explorer indefinately. Is the limitation a C# limitation?

              T D 2 Replies Last reply
              0
              • I Ian Bowler

                Why? It's a long story... The C++ code in the original post shuts down explorer indefinately. Is the limitation a C# limitation?

                T Offline
                T Offline
                tommazzo
                wrote on last edited by
                #7

                Ian Bowler wrote: The C++ code in the original post shuts down explorer indefinately. Is the limitation a C# limitation? This is, I believe, because you sent the process the WM_QUIT message, with Process.Kill() you kill it without sending it the WM_QUIT message. In order to keep explorer from restarting you could either run a loop on a new thread, which continously checks if explorer has been restarted and kills it again if neccessary (this isn't really the best idea). Or (and this should be the better idea) you could import the FindWindow and PostMessage methods with DllImport and use them the same way as in C++;

                I 1 Reply Last reply
                0
                • T tommazzo

                  Ian Bowler wrote: The C++ code in the original post shuts down explorer indefinately. Is the limitation a C# limitation? This is, I believe, because you sent the process the WM_QUIT message, with Process.Kill() you kill it without sending it the WM_QUIT message. In order to keep explorer from restarting you could either run a loop on a new thread, which continously checks if explorer has been restarted and kills it again if neccessary (this isn't really the best idea). Or (and this should be the better idea) you could import the FindWindow and PostMessage methods with DllImport and use them the same way as in C++;

                  I Offline
                  I Offline
                  Ian Bowler
                  wrote on last edited by
                  #8

                  Thanks! I'll give it a whirl... -Ian

                  1 Reply Last reply
                  0
                  • I Ian Bowler

                    Why? It's a long story... The C++ code in the original post shuts down explorer indefinately. Is the limitation a C# limitation?

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    I've got time. :-D Honestly, if an application killed off Explorer for me, without my permission, I'd want to know where on earth this person was who wrote it, for some very not-so-nice reasons... Please tell me this isn't being deployed into the real world? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                    I 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      I've got time. :-D Honestly, if an application killed off Explorer for me, without my permission, I'd want to know where on earth this person was who wrote it, for some very not-so-nice reasons... Please tell me this isn't being deployed into the real world? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                      I Offline
                      I Offline
                      Ian Bowler
                      wrote on last edited by
                      #10

                      I understand your concerns and curiosity. I'm not going to be shutting down explorer all willy-nilly. There's a good reason for it in the context of what I'm working on. For those of you that may wonder how this all turned out, I had to import the User32.dll functions, FindWindow and PostMessage (as tommazzo suggested). The final code looks like this: public class Win32 { public Win32(){} [DllImport("User32.dll")] private static extern IntPtr FindWindow(string ClassName, string WindowName); [DllImport("User32.dll")] private static extern Boolean PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); public bool ShutdownExplorer() { Boolean bSuccess = false; IntPtr Handle = new IntPtr(0); // Get a handle to explorer.exe Handle = FindWindow("Progman", null); // Shutdown explorer.exe if(Handle != IntPtr.Zero) bSuccess = PostMessage(Handle, 0x0012, IntPtr.Zero, Ptr.Zero); return bSuccess; } } Thanks tommazzo for the solution. Thanks Dave for your insights. Regards, Ian

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups