windows 如何获取系统盘符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/810273/
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 can I get the system drive letter?
提问by paxdiablo
How would I find the driver letter of the main hard disk on a Windows Operating system?
如何在 Windows 操作系统上找到主硬盘的驱动程序号?
That is, the drive with Program Files
, System32
, and so on.
也就是说,与驱动Program Files
,System32
等。
回答by paxdiablo
There's an environment variable called SystemDrive
which is set to the system drive (surprisingly enough). The getenv()
call is how you can get to it.
有一个名为的环境变量SystemDrive
被设置为系统驱动器(令人惊讶的是)。该getenv()
呼叫是你可以得到它。
char *sysDrive = getenv ("SystemDrive");
if (sysDrive == NULL) {
// vote me down.
} else {
// vote me up and use it.
}
This pagelists a whole slew of environment variables available if you can't rely on specific directories existing on the system drive.
如果您不能依赖系统驱动器上存在的特定目录,则此页面列出了大量可用的环境变量。
Alternatively, use the Windows API call, SHGetSpecialFolderPath(), and pass in the correct CSIDL. Then you shouldn't have to rely on the environment variables.
或者,使用 Windows API 调用SHGetSpecialFolderPath()并传入正确的CSIDL。那么你不应该依赖于环境变量。
Although take note on those pages that this has been superceded by other functions in Vista (it should still work since this function becomes a wrapper around the new one).
尽管在那些页面上注意这已经被 Vista 中的其他函数取代(它应该仍然有效,因为这个函数变成了新函数的包装器)。
回答by ojblass
The API Call GetWindowsDirectorycould be of assistance. You can further parse this information using API's to parse the drive letter information.
API 调用GetWindowsDirectory可能会有所帮助。您可以使用 API 进一步解析此信息以解析驱动器号信息。
回答by bsneeze
SYSTEMDRIVE
系统驱动
PROGRAMFILES
程序文件
SYSTEMROOT
系统根
WINDIR
风车
Don't assume Program Files is on the same drive as Windows. It usually is. Usually.
不要假设 Program Files 与 Windows 位于同一驱动器上。它通常是。通常。
回答by bsneeze
Never use env variables like in the wrong answer above.
env variables are updatable by the user.
永远不要像上面的错误答案那样使用环境变量。
env 变量可由用户更新。
回答by don't need to know
See Getting System Informationon MSDN. It explains how to get system information in depth for the most part. very informative.
请参阅在 MSDN 上获取系统信息。它解释了如何深入获取系统信息的大部分内容。信息量很大。