Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GetFolderPath(Environment.SpecialFolder.SOMETHING)_C#_.net - Fatal编程技术网

C# GetFolderPath(Environment.SpecialFolder.SOMETHING)

C# GetFolderPath(Environment.SpecialFolder.SOMETHING),c#,.net,C#,.net,我已经尝试了大部分的Environment.SpecialFolder枚举,但我认为没有任何方法可以仅通过枚举来实现。使用string.Substring()方法给我带来了迄今为止最远的结果 我尝试只获取系统分区路径,即windows实际安装的位置。在机器A上可能是C:\,在机器B上可能是D:\ 到目前为止,我发现最充分的解决办法是 var path = Environment.GetFolderPath(Environment.SpecialFolder.Windows)

我已经尝试了大部分的
Environment.SpecialFolder
枚举,但我认为没有任何方法可以仅通过枚举来实现。使用
string.Substring()
方法给我带来了迄今为止最远的结果

我尝试只获取系统分区路径,即windows实际安装的位置。在机器A上可能是
C:\
,在机器B上可能是
D:\

到目前为止,我发现最充分的解决办法是

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

有更好的方法吗?谢谢。

要获取驱动器,请使用
Path.GetPathRoot
。看

如果您需要“disk where Environment.SpecialFolder.Windows”,您的示例就可以了

您可能希望使用而不是Susbstring


请注意,您可能不应该自己向根驱动器写入任何内容(如果您的程序设计得很好)。

这对我来说似乎非常理想。您想改进什么?我只是想知道这个枚举中没有任何成员,它实际上返回的正是我想要的。它涵盖了很多东西,所以我只是担心,我监督了一些事情。
var root = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Windows));