45

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?

1
  • 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. Commented May 25, 2017 at 19:33

4 Answers 4

74

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

Try these:

Environment.GetEnvironmentVariable("SystemRoot")

Environment.GetEnvironmentVariable("windir")
1
  • 7
    it requires admin privileges though
    – Rafik Bari
    Commented Oct 26, 2012 at 13:27
61

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).

1
  • 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". Commented Mar 18, 2013 at 15:26
30

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.

1
  • Perfect. This should be the accepted answer because it does not require admin. Commented Jun 14, 2016 at 17:21
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");
}
5
  • doesn't that assume that this program will be in the root directory with explorer?
    – Crash893
    Commented Sep 30, 2009 at 17:20
  • 1
    @Crash893: No, that is not needed. Simply copy the code and try :-) Commented Sep 30, 2009 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
    Commented Oct 1, 2009 at 15:56
  • @divo ps. it works but im not sure how it knows where explorer.exe is
    – Crash893
    Commented Oct 1, 2009 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"); Commented Oct 1, 2009 at 18:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.