我已经尝试了大部分的Enviroment.SpecialFolder枚举,但是我认为没有任何方法可以实现我只想用枚举完成的任务。到目前为止,使用string.Substring()方法给我带来了最困难的结果。
我试图只获取系统分区路径,在那里实际上安装了windows。在机器A上可能是C:\,在机器B上可能是D:\。
到目前为止,我发现最充分的解决方案是
var path = Environment.GetFolderPath(Environment.SpecialFolder.Windows)
.Substring(0, 3);有更好的方法吗?谢谢。
发布于 2013-02-05 17:58:21
要获得驱动器,请使用Path.GetPathRoot。请参阅http://msdn.microsoft.com/en-us/library/system.io.path.getpathroot.aspx
var root = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Windows));发布于 2013-02-05 17:59:09
如果您需要“磁盘所在的Environment.SpecialFolder.Windows”,您的示例是可以的。
您可能想使用Path.GetPathRoot而不是Susbstring..。
请注意,您可能不应该自己写任何东西到根驱动器(如果您的程序设计得很好的话)。
https://stackoverflow.com/questions/14713658
复制相似问题