Page 1 of 1

Restart Windows Explorer plugin.

Posted: Thu Apr 21, 2016 3:54 am
by bokkie
Folks,

One of the forum members asked about how you can restart the Windows Explorer. I did some research on the subject and I wrote a plugin which is attached. It seems to work okay (at least on my machine). Unzip the file, run the installer and in the MSIcode you'll see a plugin called Restart Windows Explorer.

Peter.

Edit 22/April/2016: New plugin uploaded. I fixed a teeny glitch where the dialog Cancel button wasn't cancelling correctly. It now does. :oops:

Re: Restart Windows Explorer plugin.

Posted: Tue May 03, 2016 12:18 pm
by SteveDude
Thanks Peter! I do shell extensions so that will be handy. I attached an EXE I've been using to accomplish the task.

Re: Restart Windows Explorer plugin.

Posted: Wed May 04, 2016 3:09 am
by bokkie
Thanks Steve. I ran your exe and it got me thinking about how you achieved your restart?

In my plugin, the nitty-gritty is done with:

foreach(var p in Process.GetProcesses().Where(p => p.ProcessName.ToLower().EndsWith("explorer")))
{
p.Kill();
break;
}
Process.Start("explorer.exe");

Does your exe do something similar? I'd be interested to know if there are other ways of doing it. I know the WMI namespace offers classes that can discover process names using more reliable methods. From what I could find, the Process class seems to be where the smart money is?

Re: Restart Windows Explorer plugin.

Posted: Thu May 19, 2016 5:18 pm
by SteveDude
Sorry it took me so long to reply. Been on vacation and goofing off, but yes, I'm doing pretty much the same thing...

Code: Select all

using System;
using System.Diagnostics;
using System.Threading;

namespace RestartExplorer
{
    internal class RestartExplorer
    {
        public RestartExplorer()
        {
        }

        private static bool IsExplorerRunning()
        {
            Process[] processes = Process.GetProcesses();
            for (int i = 0; i < (int)processes.Length; i++)
            {
                if (processes[i].get_ProcessName().ToLower().StartsWith("explore"))
                {
                    return true;
                }
            }
            return false;
        }

        private static void KillExplorerProcesses()
        {
            Process[] processes = Process.GetProcesses();
            for (int i = 0; i < (int)processes.Length; i++)
            {
                Process process = processes[i];
                if (process.get_ProcessName().ToLower().StartsWith("explore"))
                {
                    process.Kill();
                }
            }
        }

        [STAThread]
        private static void Main(string[] args)
        {
            try
            {
                Process.EnterDebugMode();
                RestartExplorer.KillExplorerProcesses();
                Thread.Sleep(3000);
                RestartExplorer.VerifyExplorerRunning();
                if ((int)args.Length > 0)
                {
                    string[] strArray = args;
                    for (int i = 0; i < (int)strArray.Length; i++)
                    {
                        string str = strArray[i];
                        ProcessStartInfo processStartInfo = new ProcessStartInfo("explorer.exe", string.Concat("/n,/e,", str));
                        processStartInfo.set_UseShellExecute(true);
                        Process.Start(processStartInfo);
                    }
                }
                Process.LeaveDebugMode();
            }
            catch (Exception exception)
            {
            }
        }

        private static void VerifyExplorerRunning()
        {
            DateTime now = DateTime.get_Now();
            while (!RestartExplorer.IsExplorerRunning())
            {
                Thread.Sleep(200);
                if ((DateTime.get_Now() - now).get_TotalMilliseconds() <= 5000)
                {
                    continue;
                }
                Process.Start("explorer.exe");
            }
        }
    }
}


Re: Restart Windows Explorer plugin.

Posted: Fri May 20, 2016 2:58 am
by bokkie
Thanks. I was thinking of shopping around to use something else but I'm happy knowing that we both more or less do the same thing. :)

Re: Restart Windows Explorer plugin.

Posted: Fri May 20, 2016 1:09 pm
by SteveDude
...I was going to take it a step further using LockWindowUpdate so you wouldn't get the flash when Explorer is restarted but haven't went there yet.

Re: Restart Windows Explorer plugin.

Posted: Sat May 21, 2016 10:36 am
by bokkie
LockWindowUpdate? I haven't come across that one before. I'll have a look for information about it and see about adding it to the plugin. Thanks for the heads-up on it.

Edit: I have an entry for it in my copy of Dan Appleman's book on the Windows API.

Re: Restart Windows Explorer plugin.

Posted: Sat May 21, 2016 1:05 pm
by SteveDude
Just use it with the Desktop Handle and then 0 to unlock. I use it every so often displaying complex dialogs.

Re: Restart Windows Explorer plugin.

Posted: Sun May 22, 2016 5:07 pm
by bokkie
You mean something like the following?

var handle = GetDesktopWindow();
LockWindowUpdate(handle);
...
LockWindowUpdate((IntPtr) 0);

When I run this code the bit in the middle, the ... is where I have my code to restart the explorer. It still flashes on the monitor. My definition for the GetDesktopWindow and LockWindowUpdate methods is correctly defined. My window handle returns a positive IntPtr value. Anything obvious that I'm missing?

By the way, you could have told me that stepping into the code where the ... is does nothing, because the desktop handle is locked and with it, so is every window on the desktop. :D