A Blue Star

Get special folders with SHGetKnownFolderPath

Posted on December 16th, 2009 by Steven Smethurst

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;
}

This entry was posted on Wednesday, December 16th, 2009 at 3:04 pm and is filed under snippet. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Response

Add Tag