Restart Windows Explorer plugin.

Interested in developing new plug-ins? Got one to share? Post here!
bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Restart Windows Explorer plugin.

Postby bokkie » Thu Apr 21, 2016 3:54 am

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:
Attachments
Restart Windows Explorer plugin.7z
(2.59 MiB) Downloaded 1472 times
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP

SteveDude
Posts: 253
Joined: Wed Apr 11, 2007 6:07 pm

Re: Restart Windows Explorer plugin.

Postby SteveDude » Tue May 03, 2016 12:18 pm

Thanks Peter! I do shell extensions so that will be handy. I attached an EXE I've been using to accomplish the task.
Attachments
RestartExplorer.zip
(6.31 KiB) Downloaded 1444 times

bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Re: Restart Windows Explorer plugin.

Postby bokkie » Wed May 04, 2016 3:09 am

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?
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP

SteveDude
Posts: 253
Joined: Wed Apr 11, 2007 6:07 pm

Re: Restart Windows Explorer plugin.

Postby SteveDude » Thu May 19, 2016 5:18 pm

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


bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Re: Restart Windows Explorer plugin.

Postby bokkie » Fri May 20, 2016 2:58 am

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. :)
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP

SteveDude
Posts: 253
Joined: Wed Apr 11, 2007 6:07 pm

Re: Restart Windows Explorer plugin.

Postby SteveDude » Fri May 20, 2016 1:09 pm

...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.

bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Re: Restart Windows Explorer plugin.

Postby bokkie » Sat May 21, 2016 10:36 am

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.
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP

SteveDude
Posts: 253
Joined: Wed Apr 11, 2007 6:07 pm

Re: Restart Windows Explorer plugin.

Postby SteveDude » Sat May 21, 2016 1:05 pm

Just use it with the Desktop Handle and then 0 to unlock. I use it every so often displaying complex dialogs.

bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Re: Restart Windows Explorer plugin.

Postby bokkie » Sun May 22, 2016 5:07 pm

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
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP


Return to “Plug-In Development”

Who is online

Users browsing this forum: No registered users and 3 guests