How can I get the system drive letter?

孤人 提交于 2019-11-30 22:59:27

问题


How would I find the driver letter of the main hard disk on a Windows Operating system?

That is, the drive with Program Files, System32, and so on.


回答1:


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.

char *sysDrive = getenv ("SystemDrive");
if (sysDrive == NULL) {
    // vote me down.
} else {
    // vote me up and use it.
}

This page lists 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.

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).




回答2:


The API Call GetWindowsDirectory could be of assistance. You can further parse this information using API's to parse the drive letter information.




回答3:


SYSTEMDRIVE

PROGRAMFILES

SYSTEMROOT

WINDIR

Don't assume Program Files is on the same drive as Windows. It usually is. Usually.




回答4:


Never use env variables like in the wrong answer above.
env variables are updatable by the user.




回答5:


See Getting System Information on MSDN. It explains how to get system information in depth for the most part. very informative.



来源:https://stackoverflow.com/questions/810273/how-can-i-get-the-system-drive-letter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!