Search code examples
.netwindowswinapiexplorerwindows-explorer

How to programmatically restart windows explorer process


I'm working on a windows shell extension, and unfortunately, when making changes to the DLL, I must restart windows explorer (since it keeps the DLL in memory).

I found this program from Dino Esposito, but it doesn't work for me.

void SHShellRestart(void)
{
    HWND hwnd;
    hwnd = FindWindow("Progman", NULL );
    PostMessage(hwnd, WM_QUIT, 0, 0 );
    ShellExecute(NULL, NULL, "explorer.exe", NULL, NULL, SW_SHOW );
    return;
}

Does any one have something they can share to do this?

P.S. I realize that I can go to task manager and kill the explorer process, but I just want to do it the lazy way. Besides, this enables automation.

P.P.S I am using .NET for the development, but the shell restart functionality could be in C, C++ or a .NET language. It will simply be a small stand-alone executable.


Solution

  • A fool-proof solution:

    foreach (Process p in Process.GetProcesses())
    {
        // In case we get Access Denied
        try
        {
            if (p.MainModule.FileName.ToLower().EndsWith(":\\windows\\explorer.exe"))
            {
                p.Kill();
                break;
            }
        }
        catch
        { }
    }
    Process.Start("explorer.exe");