Environment.GetFolderPath(Environment.SpecialFolder.SOMETHING)

Mic*_*ing 3 .net c#

我一直在尝试大部分的Enviroment.SpecialFolder枚举,但我认为只有枚举才能实现我想要完成的任何方式.使用这种string.Substring()方法给我带来了最重要的.

我试着只获得系统分区路径,其中实际安装了Windows.在机器A上它可能是C:\,在机器B上它可能是D:\.

到目前为止,我找到了最充分的解决方案

var path = Environment.GetFolderPath(Environment.SpecialFolder.Windows)
                      .Substring(0, 3);
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?谢谢.

Ric*_*der 8

要获得驱动器,请使用Path.GetPathRoot.请参阅http://msdn.microsoft.com/en-us/library/system.io.path.getpathroot.aspx

var root = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Windows));
Run Code Online (Sandbox Code Playgroud)