How to programmatically restart windows explorer process How to programmatically restart windows explorer process windows windows

How to programmatically restart windows explorer process


After parsing some of the earlier answers and doing a bit of research, I've created a little complete example in C#. This closes the explorer shell then waits for it to completely shut down and restarts it. Hope this helps, there's a lot of interesting info in this thread.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.Diagnostics;using System.Threading;namespace RestartExplorer{class Program{    [DllImport("user32.dll", SetLastError = true)]    static extern bool PostMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] uint Msg, IntPtr wParam, IntPtr lParam);    [DllImport("user32.dll", SetLastError = true)]    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);    const int WM_USER = 0x0400; //http://msdn.microsoft.com/en-us/library/windows/desktop/ms644931(v=vs.85).aspx    static void Main(string[] args)    {        try        {            var ptr = FindWindow("Shell_TrayWnd", null);            Console.WriteLine("INIT PTR: {0}", ptr.ToInt32());            PostMessage(ptr, WM_USER + 436, (IntPtr)0, (IntPtr)0);            do            {                ptr = FindWindow("Shell_TrayWnd", null);                Console.WriteLine("PTR: {0}", ptr.ToInt32());                if (ptr.ToInt32() == 0)                {                    Console.WriteLine("Success. Breaking out of loop.");                    break;                }                Thread.Sleep(1000);            } while (true);        }        catch (Exception ex)        {            Console.WriteLine("{0} {1}", ex.Message, ex.StackTrace);        }        Console.WriteLine("Restarting the shell.");        string explorer = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), "explorer.exe");        Process process = new Process();                   process.StartInfo.FileName = explorer;        process.StartInfo.UseShellExecute = true;        process.Start();        Console.ReadLine();    }}}


I noticed no one addressed the issue of starting explorer.exe as the shell, rather than it just opening an explorer window. Took me a while to figure this out, turns out it was something simple:

string explorer = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), "explorer.exe");        Process process = new Process();        process.StartInfo.FileName = explorer;        process.StartInfo.UseShellExecute = true;        process.Start();

You have to set the StartInfo.UseshellExecute as true to get it to restart as the shell.


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