views:

735

answers:

2

I am writing a program to kill and restart explorer but I don't want to hard code the location becuase some people install windows in diffrent 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

anyone know what the best way to do this is.

+11  A: 

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

Try these:

Environment.GetEnvironmentVariable("SystemRoot")

Environment.GetEnvironmentVariable("windir")
Baddie
ding ding ding we have a winar!!! (+1 to you)
Crash893
+3  A: 

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");
}
0xA3
doesn't that assume that this program will be in the root directory with explorer?
Crash893
+1 : For a better implementation suggestion
Ian
@Crash893: No, that is not needed. Simply copy the code and try :-)
0xA3
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
@divo ps. it works but im not sure how it knows where explorer.exe is
Crash893
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");`
0xA3