问题
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