Skip to content

Instantly share code, notes, and snippets.

@abel0b
Created November 23, 2020 19:09

Revisions

  1. abel0b created this gist Nov 23, 2020.
    23 changes: 23 additions & 0 deletions appdata.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    // 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;
    }