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.
Restart Windows Explorer plugin.
Restart Windows Explorer plugin.
- Attachments
-
- Restart Windows Explorer plugin.7z
- (2.59 MiB) Downloaded 1472 times
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP
InstallAware MVP
Re: Restart Windows Explorer plugin.
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
Re: Restart Windows Explorer plugin.
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?
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
InstallAware MVP
Re: Restart Windows Explorer plugin.
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.
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
InstallAware MVP
Re: Restart Windows Explorer plugin.
...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.
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.
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
InstallAware MVP
Re: Restart Windows Explorer plugin.
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.
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.
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.
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP
InstallAware MVP
Return to “Plug-In Development”
Who is online
Users browsing this forum: No registered users and 3 guests