Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32 function to get path to C:\ProgramData

My app needs to install some files that can be edited by the application at run time. Install shield provides an alias [CommonAppDataFolder] that will resolve to c:\programData on Vista and Windows 7 and will also work on Windows XP. Is there a win32 function that will return a similar path?

Maybe I need to call different functions depending on the operating system?

like image 670
Mike2012 Avatar asked Jun 25 '12 19:06

Mike2012


People also ask

How do I find the ProgramData path?

To view the "ProgramData" folder you will need to go to the Windows control panel , select "Appearance and Personalization", and find the "folder options" dialog. Select the View Tab, make the changes shown above, and click OK. You should now be able to see and access the "ProgramData" folder.

Where can I find program data in Windows XP?

On Windows XP, there was no C:\ProgramData folder. Instead, there was a “C:\Documents and Settings\All Users\Application Data” folder. Starting with Windows Vista, the All Users application data folder was moved to C:\ProgramData. You can still see this today.

What is SHGetFolderPath?

The known folder system that underlies SHGetFolderPath allows users or administrators to redirect a known folder to a location that suits their needs.


1 Answers

SHGetFolderPath/SHGetSpecialFolderPath get you that, with CSIDL_COMMON_APPDATA argument.

See code snippet here (at the bottom): How to write a Windows XP Application that stores user and application data in the correct location by using Visual C++ ; the original link is no longer valid - code snippet is pulled below):

include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")
void CreateTemporaryFile()
{
   TCHAR szPath[MAX_PATH];
   // Get path for each computer, non-user specific and non-roaming data.
   if ( SUCCEEDED( SHGetFolderPath( NULL, CSIDL_COMMON_APPDATA, 
                                    NULL, 0, szPath ) ) )
   {
      TCHAR szTempFileName[MAX_PATH];
      // Append product-specific path - this path needs to already exist
      // for GetTempFileName to succeed.
      PathAppend( szPath, _T("\\My Company\\My Product\\1.0\\") );
      // Generate a temporary file name within this folder.
      if (GetTempFileName( szPath, 
                           _T("PRE"),
                           0,
                           szTempFileName ) != 0 )
      {
         HANDLE hFile = NULL;
         // Open the file.
         if (( hFile = CreateFile( szTempFileName, 
                                   GENERIC_WRITE, 
                                   0, 
                                   NULL, 
                                   CREATE_ALWAYS, 
                                   FILE_ATTRIBUTE_NORMAL, 
                                   NULL )) != INVALID_HANDLE_VALUE )
         {
            // Write temporary data (code omitted).
            CloseHandle( hFile );
         }
      }
      else
          DWORD err = GetLastError();
   }
}
  • Windows XP: C:\Documents and Settings\All Users\Application Data
  • Windows Vista: C:\ProgramData
  • Windows 7: C:\ProgramData

See also: CSIDL.

like image 141
Roman R. Avatar answered Oct 02 '22 10:10

Roman R.