Click here to Skip to main content
15,896,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
This below is the code to restart explorer.exe and it works.

I would like to after the restart explorer.exe, it happens reopen the same windows, 
if they were opened before restart explorer.exe. Thank you.


What I have tried:

private void restartexplorer()
        {
            const string explorer = "explorer.exe";
            string explorerPath = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), explorer);
            foreach (Process process in Process.GetProcesses())
            {
                // In case we get Access Denied
                try
                {
                    if (string.Compare(process.MainModule.FileName, explorerPath, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        process.Kill();
                    }
                }
                catch
                {
                  //
                }
            }       
        }
Posted
Updated 23-Mar-17 2:26am
Comments
ZurdoDev 23-Mar-17 8:15am    
Then store off the open windows and reopen them. Where are you stuck?
ZurdoDev 23-Mar-17 8:16am    
Also, have you actually tried this code?

1. I doubt your code has permissions to kill explorer.
2. I'm pretty sure the existing windows stay open anyway.
Dave Kreskowiak 23-Mar-17 9:12am    
Actually, the code will have permissions to kill explorer. Explorer runs as the user logged in, not the system.

On a system where multiple users are logged in, he can only kill his copy of Explorer. He won't be able to kill anyone elses.
ZurdoDev 23-Mar-17 9:25am    
OK, makes sense. I'll trust you.
Richard MacCutchan 23-Mar-17 8:23am    
What exactly is the purpose of this code?

1 solution

The shown code does not start the Explorer and you can't restart it. All you can do is starting another instance.

If you want to open the Explorer for a specifc directory, just pass that as command line argument:
explorer <directory>

But it might be difficult to get the folders that had been opened before. Depending on the Windows versions you might be able to get a list of recent folders or items from the registry or shell:Recent.
 
Share this answer
 
Comments
Member 10410972 23-Mar-17 9:35am    
Okay now I understand. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900