Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find windows folder programmatically in c#

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?

like image 489
Crash893 Avatar asked Sep 30 '09 16:09

Crash893


3 Answers

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

Try these:

Environment.GetEnvironmentVariable("SystemRoot")

Environment.GetEnvironmentVariable("windir")
like image 150
Omar Avatar answered Sep 20 '22 04:09

Omar


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

like image 58
Uri Avatar answered Sep 23 '22 04:09

Uri


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.

like image 28
Adam Lindsay Avatar answered Sep 21 '22 04:09

Adam Lindsay