Get special folders with SHGetKnownFolderPath

This application will print the special folders that you should be storing your applcations data in.
UAC on Windows Vista/7 will no longer alowe you to write to files in the “C:\Program Files\” with out elevation to administrator. Instead you should be writting your settings file to the “LocalAppData” folder and your output and log file to “Documents” folder.

More information on UAC

Source code:

#include <shlobj.h>
void GetSpecialFolder( REFKNOWNFOLDERID rfid ) {
LPWSTR wszPath = NULL;
HRESULT hr = SHGetKnownFolderPath ( rfid, KF_FLAG_CREATE, NULL, &wszPath );
if ( SUCCEEDED(hr) )
{
wprintf( _T("Path: %s\n"), wszPath );
}
}

int _tmain(int argc, _TCHAR* argv[])
{
GetSpecialFolder( FOLDERID_ProgramData ); // Shared program data directory for all users
GetSpecialFolder( FOLDERID_RoamingAppData ); // Per-user program data directory (roaming)
GetSpecialFolder( FOLDERID_LocalAppData ); // Per-user program data directory (non-roaming)

GetSpecialFolder( FOLDERID_ProgramFiles ); // App install directory
GetSpecialFolder( FOLDERID_Documents ); // Logs, reports, output
return 0;
}

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>