How can I get the system drive letter?
Asked Answered
V

5

18

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.

Veratrine answered 1/5, 2009 at 5:15 Comment(3)
Don't assume programs will be in "\Program Files", or system files in "\Windows\System32" - there are API calls to retrieve exact paths for these and more...Michael
Can you give me an example of one of those API calls so I can research them? I am wanting to look for certain files associated with programsVeratrine
msdn.microsoft.com/en-us/library/ms724373.aspx ... msdn.microsoft.com/en-us/library/bb762181.aspxMichael
B
32

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

Brunner answered 1/5, 2009 at 5:20 Comment(0)
T
5

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

Tolly answered 1/5, 2009 at 5:20 Comment(2)
What APIs would I use to "further parse" the GetWindowsDirectory() results?Gearalt
Just get the first letter of the windows directory path.Modulation
R
2

SYSTEMDRIVE

PROGRAMFILES

SYSTEMROOT

WINDIR

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

Rhines answered 1/5, 2009 at 5:28 Comment(1)
You should mention that those in caps are names for Windows environment variables.Gearalt
T
1

Get the system drive letter by using the function _dupenv_s .

getenv "Function is not thread safe [concurrency-mt-unsafe]" and "This function or variable may be unsafe. Consider using _dupenv_s instead." - VS2019 Compiler Warning (level 3) C4996.

#include <iostream>

int main()
{
    char* system_drive{ nullptr };
    size_t count{ 0 };

    // _dupenv_s is a Microsoft function, designed as a more secure form of getenv:
    _dupenv_s(&system_drive, &count, "SystemDrive");

    std::cout << "system drive: " << system_drive << std::endl;

    // Note: It is the calling program's responsibility to free the memory by calling free:
    free(system_drive);
}

Output:

system drive: C:
Theressathereto answered 4/7, 2022 at 10:33 Comment(0)
D
0

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

Deluna answered 24/12, 2017 at 11:46 Comment(1)
YOu need to format the answer with code tags and line breaks.Pavonine

© 2022 - 2024 — McMap. All rights reserved.