Skip to content

Instantly share code, notes, and snippets.

@abel0b
Created November 23, 2020 19:09
Show Gist options
  • Save abel0b/0b740648d6370e3e77fd70a816a34523 to your computer and use it in GitHub Desktop.
Save abel0b/0b740648d6370e3e77fd70a816a34523 to your computer and use it in GitHub Desktop.
Get AppData Roaming folder in C C++
// how to locate application data %AppData% directory path on Windows 10 MSVC
// see on documentation https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath?redirectedfrom=MSDN
// compiled with cl .\appdata.c Shell32.lib
#include <stdlib.h>
#include <stdio.h>
#include <shlobj_core.h>
#include <direct.h>
int main() {
// KF_FLAG_CREATE force folder creation if not exists
PWSTR appdata = NULL;
if (SHGetKnownFolderPath(&FOLDERID_RoamingAppData, KF_FLAG_CREATE, NULL, &appdata) == S_OK) {
char dest[MAX_PATH];
wcstombs(dest, appdata, MAX_PATH);
printf("appdata path is %s\n", dest);
}
else {
fprintf(stderr, "error getting appdata path\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment