9

As above, how do I get the AppData folder in Windows using C?

I know that for C# you use Environment.SpecialFolder.ApplicationData

Romulus
  • 579
  • 2
  • 9
  • 13

5 Answers5

15

Use SHGetSpecialFolderPath with a CSIDL set to the desired folder (probably CSIDL_APPDATA or CSIDL_LOCAL_APPDATA).

You can also use the newer SHGetFolderPath() and SHGetKnownFolderPath() functions. There's also SHGetKnownFolderIDList() and if you like COM there's IKnownFolder::GetPath().

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
7

If I recall correctly it should just be

#include <stdlib.h>
getenv("APPDATA");

Edit: Just double-checked, works fine!

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • 3
    Use the proper API instead, as environment variables on Windows are more a courtesy than a contract. They might not be present under all circumstances. However, `SHGetKnownFolderPath` *will* give you the path every time. – Joey Oct 18 '10 at 23:11
  • But SHGetKnownFolderPath might not be available either. Tough cookies. – Hans Passant Oct 18 '10 at 23:46
  • @Hans: SHGetSpecialFolderPath should be, though. Depending on the OS one targets. – Joey Oct 19 '10 at 06:19
4

Using the %APPDATA% environment variable will probably work most of the time. However, if you want to do this the official Windows way, you should use use the SHGetFolderPath function, passing the CSIDL value CSIDL_APPDATA or CSIDL_LOCAL_APPDATA, depending on your needs.

This is what the Environment.GetFolderPath() method is using in .NET.

EDIT: Joey correctly points out that this has been replaced by SHGetKnownFolderPath in Windows Vista. News to me :-).

Matt Solnit
  • 32,152
  • 8
  • 53
  • 57
1

You might use these functions:

#include <stdlib.h>
char *getenv( 
   const char *varname 
);
wchar_t *_wgetenv( 
   const wchar_t *varname 
);

Like so:

#include <stdio.h>
char *appData = getenv("AppData");
printf("%s\n", appData);
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • 3
    Use the proper API instead, as environment variables on Windows are more a courtesy than a contract. They might not be present under all circumstances. However, `SHGetKnownFolderPath` *will* give you the path every time. – Joey Oct 18 '10 at 23:12
  • 3
    It's useful to be able to override environment variables on a per-process basis. Why do you say they are "more a courtesy than a contract"? Is there some official or quasi-official documentation to this effect? – Integer Poet Feb 22 '11 at 18:37
1

Sample code from MSDN:

TCHAR szPath[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL,
   CSIDL_APPDATA | CSIDL_FLAG_CREATE,
   NULL,
   0,
   szPath)))
{
   PathAppend(szPath, TEXT("MySettings.xml"));
   HANDLE hFile = CreateFile(szPath, ...);
}

CSIDL_APPDATA = username\Application Data. In Window 10 is: username\AppData\Roaming

CSIDL_FLAG_CREATE = combine with CSIDL_ value to force folder creation in SHGetFolderPath()

You can also use:

CSIDL_LOCAL_APPDATA = username\Local Settings\Application Data (non roaming)

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Thiago Falcao
  • 4,463
  • 39
  • 34