Michael Schnerring
Michael Schnerring

Reputation: 3661

Environment.GetFolderPath(Environment.SpecialFolder.SOMETHING)

I've been trying out the most of the Enviroment.SpecialFolder enumeration, but I think there isn't any way of what I'd like to accomplish with the enumeration only. Using the string.Substring() method brought me the farest, yet.

I try to get just the system partition path, where windows is actually installed. On machine A it might be C:\, on machine B it might be D:\.

The most sufficent solution, I found so far was

var path = Environment.GetFolderPath(Environment.SpecialFolder.Windows)
                      .Substring(0, 3);

Is there a better way to do this? Thanks.

Upvotes: 3

Views: 2570

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100610

If you need "disk where Environment.SpecialFolder.Windows" your sample is ok.

You may want to use Path.GetPathRoot instead of Susbstring...

Note that you probably should not write anything to the root drive yourself (if your program is designed to behave nice).

Upvotes: 3

Richard Schneider
Richard Schneider

Reputation: 35477

To get the drive, use Path.GetPathRoot. See http://msdn.microsoft.com/en-us/library/system.io.path.getpathroot.aspx

var root = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Windows));

Upvotes: 9

Related Questions