C# 您如何检测主硬盘驱动器盘符,例如 C: 驱动器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/588990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you detect the main hard drive letter such as C: drive?
提问by
How do you detect the main hard drive letter such as C: drive?
您如何检测主硬盘驱动器盘符,例如 C: 驱动器?
采纳答案by Ray
Try
尝试
Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System));
That will return (normally) C:\
这将返回(通常)C:\
But it depends on how you define the "main" hard drive. This will give you the drive Windows is installed on.
但这取决于您如何定义“主”硬盘。这将为您提供安装 Windows 的驱动器。
回答by goldenratio
var d = Directory.GetDirectoryRoot(Environment.GetEnvironmentVariable("windir"));
回答by Andrew Flanagan
回答by cdonner
"SystemDrive" is the environment variable on XP that you want.
“SystemDrive”是您想要的 XP 环境变量。
回答by Wedge
This should work (assuming you want the drive that windows is on):
这应该可以工作(假设您想要 Windows 所在的驱动器):
string rootDrive = Path.GetPathRoot(Environment.SystemDirectory);