views:

327

answers:

4

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, etc.

+3  A: 

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

ojblass
+7  A: 

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

paxdiablo
+1 for simplicity....
ojblass
Joel would be proud ;)
Chad Grant
+1  A: 

SYSTEMDRIVE

PROGRAMFILES

SYSTEMROOT

WINDIR

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

pyrochild
A: 

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

Sure, the user can change the values of %WINDIR%, etc., but the user can also mess with their registry, replace all your application's resource files with their LOLCats, or install Linux. Those are really not things your app needs to have a contingency plan for.
pyrochild