44

I am writing a program to kill and restart explorer but I don't want to hard code the location because some people install windows in different places (for example I found someone who had it installed in the d:\ drive where the C:\ drive did exist but had nothing installed on it)

I tried looking under Environment.SpecialFolder. but I don't see a "windows" option under that

What is the best way to do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Crash893
  • 11,428
  • 21
  • 88
  • 123
  • 2
    would you consider changing the accepted answer to the one I wrote, it doesn't require admin and supports lower versions of the .net framework. – Adam Lindsay May 25 '17 at 19:33

4 Answers4

73

http://msdn.microsoft.com/en-us/library/77zkk0b6.aspx

Try these:

Environment.GetEnvironmentVariable("SystemRoot")

Environment.GetEnvironmentVariable("windir")
Omar
  • 39,496
  • 45
  • 145
  • 213
60

Environment.GetFolderPath( Environment.SpecialFolder.Windows ) will return the path to the Windows folder. Recommend this approach over the environment variable, because using an API that does exactly what we want (.NET 4.0 and above).

Uri London
  • 10,631
  • 5
  • 51
  • 81
  • 2
    You should point out in your response that Environment.SpecialFolder.Windows does not exist on .NET versions < 4.0. You also have a typo in "SpecialFolder". – Jason Slocomb Mar 18 '13 at 15:26
28

I would highly recommend the use of:

Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System))

It does NOT require administrator rights and supports all versions of the .NET framework.

Adam Lindsay
  • 396
  • 3
  • 10
10

To simply kill and restart Windows Explorer you wouldn't need the path to the system folder as this is already included in the PATH environment variable (unless the user messed with it).

That short program will kill all explorer.exe instances and then restart explorer.exe:

static void Main(string[] args)
{
    foreach (Process process in Process.GetProcessesByName("explorer"))
    {
        if (!process.HasExited)
        {
            process.Kill();
        }
    }
    Process.Start("explorer.exe");
}
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • doesn't that assume that this program will be in the root directory with explorer? – Crash893 Sep 30 '09 at 17:20
  • 1
    @Crash893: No, that is not needed. Simply copy the code and try :-) – Dirk Vollmar Sep 30 '09 at 17:24
  • 1
    I'll give you the plus one but its not the answer to the question but i do appreciate you taking a look at the bigger problem – Crash893 Oct 01 '09 at 15:56
  • @divo ps. it works but im not sure how it knows where explorer.exe is – Crash893 Oct 01 '09 at 16:00
  • It works because the system root folder ("C:\Windows") is included in your path environment variable. Open a command prompt and type `echo %PATH%`, then you will see that C:\Windows is printed within the ';'-separated list of directories. All folders that are in your path variable are searched when you execute a command on the shell. Nothing else happens when you do `Process.Start("explorer.exe");` – Dirk Vollmar Oct 01 '09 at 18:10