How can I get the system drive letter?

丶灬走出姿态 提交于 2019-12-01 02:57:42

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

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

SYSTEMDRIVE

PROGRAMFILES

SYSTEMROOT

WINDIR

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

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

don't need to know

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

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