Hey, I'm a bit new to C++ and am writing a simple program. My program will be using some folders in
Windows 7 path: C:\Users\%username%\Appdata\Local...
Windows XP path: C:\Documents and Settings\%username%\Local Settings\Application Data...
Unix: /home/%username%/.hiddenfolder/...
now the problem is windows. In my header file, I can do a nice
#ifdef _WIN32
to differentiate from windows and unix versions of the program, but during runtime I need to find if the user is useing XP or Vista/7 to set a correct path. Is there a standard way of doing this?
You don't need OS version at all.
On *nixes (well, on Linux and OSX for sure, but should be on others too) you can use HOME
environment variable. On Windows, you must (yes, must, because paths can be remapped/localised and hard-coding them is nice way to having more work than necessary) use SHGetFolderPath
function (it's marked as deprecated, but it's not going anywhere any time soon, and newer SHGetKnownFolderPath
is >=Vista), e.g.
TCHAR buffer[MAX_PATH];
HRESULT res = SHGetFolderPath(
NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, buffer
);
if (SUCCEEDED(res)) {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With