Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:data_and_configuration_folders [2011/04/01 09:37]
smayr [Managed Code]
swdev:dotnet:data_and_configuration_folders [2011/04/01 16:08] (current)
smayr [InnoSetup]
Line 1: Line 1:
 == Data and Configuration Folders == == Data and Configuration Folders ==
  
-Source: [[http://blogs.msdn.com/b/patricka/archive/2010/03/18/where-should-i-store-my-data-and-configuration-files-if-i-target-multiple-os-versions.aspx|Where Should I Store my Data and Configuration Files if I Target Multiple OS Versions? by Pat Altimore]]+Source: [[http://blogs.msdn.com/b/patricka/archive/2010/03/18/where-should-i-store-my-data-and-configuration-files-if-i-target-multiple-os-versions.aspx|Altimore, Pat. "Where Should I Store my Data and Configuration Files if I Target Multiple OS Versions?"]]
  
 === Where Should I Store my Data and Configuration Files if I Target Multiple OS Versions? === === Where Should I Store my Data and Configuration Files if I Target Multiple OS Versions? ===
Line 11: Line 11:
 Where to store application files depends on how that data is used by the application and the user. Here is a table to outline the recommended locations for user documents and configuration data.  I’ve mapped the locations across OS’s in this giant table for comparison. UPDATE: The table was a little too giant for the new MSDN template. I’ve reformatted the information into sections: Where to store application files depends on how that data is used by the application and the user. Here is a table to outline the recommended locations for user documents and configuration data.  I’ve mapped the locations across OS’s in this giant table for comparison. UPDATE: The table was a little too giant for the new MSDN template. I’ve reformatted the information into sections:
  
-Per user configuration files synchronized across domain joined machines via Active Directory Roaming+===== Per user configuration files synchronized across domain joined machines via Active Directory Roaming ===== 
 Configuration data files that the application uses and are unique per user. This per user data is synchronized across the domain via Active Directory. Configuration data files that the application uses and are unique per user. This per user data is synchronized across the domain via Active Directory.
  
Line 25: Line 26:
 </code>  </code> 
  
-==== Local per user configuration files ====+===== Local per user configuration files =====
  
 Configuration data files that the application uses and is unique per user.  It stays local to the individual machine and is not synchronized via Active Directory. Configuration data files that the application uses and is unique per user.  It stays local to the individual machine and is not synchronized via Active Directory.
Line 40: Line 41:
 </code> </code>
  
-==== Per machine configuration data ====+===== Per machine configuration data =====
  
 Configuration data files the application uses and is per machine. It is used across all users of the application. Configuration data files the application uses and is per machine. It is used across all users of the application.
Line 54: Line 55:
 </code> </code>
  
-==== Per user “Documents” ====+===== Per user “Documents” =====
  
 “Document” type files that users create/open/close/save in the application that are per user. “Document” type files that users create/open/close/save in the application that are per user.
Line 68: Line 69:
 </code>  </code> 
  
-==== Per Machine “Documents” ====+===== Per Machine “Documents” ====
  
 “Document” type files that users create/open/close/save in the application that are used across users.  These are usually template or public documents. “Document” type files that users create/open/close/save in the application that are used across users.  These are usually template or public documents.
Line 86: Line 87:
 ==== Targeting Vista and Higher ==== ==== Targeting Vista and Higher ====
  
-Native Code+===== Native Code ===== 
 The best API to use if you are targeting Vista and beyond is the new SHGetKnownFolderPath. This function replaces SHGetFolderPath and has the following advantages. The best API to use if you are targeting Vista and beyond is the new SHGetKnownFolderPath. This function replaces SHGetFolderPath and has the following advantages.
  
Line 96: Line 98:
                                     NULL, &wszPath )))                                     NULL, &wszPath )))
 { {
-    printf("\nSHGetKnownFolderPath FOLDERID_RoamingAppData    =%S\n", wszPath);+    printf("\nSHGetKnownFolderPath FOLDERID_RoamingAppData    = %S\n", wszPath);
 } }
  
Line 104: Line 106:
                                     NULL, &wszPath )))                                     NULL, &wszPath )))
  
-    printf("SHGetKnownFolderPath FOLDERID_Documents        =%S\n", wszPath);+    printf("SHGetKnownFolderPath FOLDERID_Documents        = %S\n", wszPath);
 } }
 </code> </code>
Line 110: Line 112:
 The output on Windows 7 is: The output on Windows 7 is:
 <code> <code>
-SHGetKnownFolderPath FOLDERID_RoamingAppData =C:\Users\patricka\AppData\Roaming +SHGetKnownFolderPath FOLDERID_RoamingAppData = C:\Users\patricka\AppData\Roaming 
-SHGetKnownFolderPath FOLDERID_Documents      =\\zaw\Mydocs\patricka\My Documents+SHGetKnownFolderPath FOLDERID_Documents      = \\zaw\Mydocs\patricka\My Documents
 </code> </code>
  
-==== Managed Code ====+===== Managed Code =====
  
 You can use System.Environment.SpecialFolder along with the System.Environment.GetFolderPath function to get the path. You can use System.Environment.SpecialFolder along with the System.Environment.GetFolderPath function to get the path.
Line 141: Line 143:
 ==== Targeting XP and Higher ==== ==== Targeting XP and Higher ====
  
-Native Code+===== Native Code ===== 
 If you are still targeting XP, you’ll need to use the legacy API SHGetFolderPath that uses CSIDL’s.  This API is still supported in Vista and Windows 7 and will map to the correct locations on all three versions of the OS. If you are still targeting XP, you’ll need to use the legacy API SHGetFolderPath that uses CSIDL’s.  This API is still supported in Vista and Windows 7 and will map to the correct locations on all three versions of the OS.
  
Line 153: Line 156:
                              szPath)))                               szPath))) 
  
-    printf("\nSHGetFolderPath CSIDL_APPDATA    =%S\n", szPath); +    printf("\nSHGetFolderPath CSIDL_APPDATA    = %S\n", szPath); 
 } }
  
Line 163: Line 166:
                              szPath)))                               szPath))) 
  
-    printf("\nSHGetFolderPath CSIDL_MYDOCUMENTS    =%S\n", szPath); +    printf("\nSHGetFolderPath CSIDL_MYDOCUMENTS    = %S\n", szPath); 
 } }
 </code> </code>
Line 169: Line 172:
 The output on Windows 7 is: The output on Windows 7 is:
 <code> <code>
-SHGetFolderPath CSIDL_APPDATA     =C:\Users\patricka\AppData\Roaming +SHGetFolderPath CSIDL_APPDATA     = C:\Users\patricka\AppData\Roaming 
-SHGetFolderPath CSIDL_MYDOCUMENTS =\\zaw\Mydocs\patricka\My Documents+SHGetFolderPath CSIDL_MYDOCUMENTS = \\zaw\Mydocs\patricka\My Documents
 </code> </code>
  
 The output on XP is: The output on XP is:
 <code> <code>
-SHGetFolderPath CSIDL_APPDATA     =C:\Documents and Settings\XPMUser\Application Data +SHGetFolderPath CSIDL_APPDATA     = C:\Documents and Settings\XPMUser\Application Data 
-SHGetFolderPath CSIDL_MYDOCUMENTS =C:\Documents and Settings\XPMUser\My Documents+SHGetFolderPath CSIDL_MYDOCUMENTS = C:\Documents and Settings\XPMUser\My Documents
 </code> </code>
  
Line 185: Line 188:
 Since we are using the .NET framework, we can still use the same technique as described in the previous section. We saw the output for Windows 7. Here’s the output we get on XP: Since we are using the .NET framework, we can still use the same technique as described in the previous section. We saw the output for Windows 7. Here’s the output we get on XP:
 <code> <code>
-SpecialFolder.ApplicationData path  =C:\Documents and Settings\XPMUser\Application Data +SpecialFolder.ApplicationData path  = C:\Documents and Settings\XPMUser\Application Data 
-SpecialFolder.MyDocuments path      =C:\Documents and Settings\XPMUser\My Documents+SpecialFolder.MyDocuments path      = C:\Documents and Settings\XPMUser\My Documents
 </code> </code>
  
Line 194: Line 197:
  
 Think about the type of files your application is writing.  Are they configuration files, data files, or documents? Are they per machine or per user? Choose the appropriate location for the file type and then use the right API to get the path to that location. Chris has a great post on this topic. There is also a support article and a whitepaper that has good info on storing application data. Think about the type of files your application is writing.  Are they configuration files, data files, or documents? Are they per machine or per user? Choose the appropriate location for the file type and then use the right API to get the path to that location. Chris has a great post on this topic. There is also a support article and a whitepaper that has good info on storing application data.
 +
 +== Best Practices ==
 +
 +Store only application files in ''C:\Program Files''.
 +Store share application data in ''C:\ProgramData''.
 +Store user data in ''C:\Users''.
 +
 +==== Coding in C# ====
 +Use ''Environment.SpecialFolder'' to get these folders. Example:
 +<code csharp>
 +string userAppData   = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);  
 +string commonAppData = Envrionment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
 +</code>
 +
 +==== InnoSetup ====
 +Set proper permissions for shared ''C:\ProgramData'' folder to store share database and configuration files.  The permissions should be Read, Write, Modify for user group ''Users'':
 +<code inno>
 +[Dirs]
 +Name: {commonappdata}\Acme\MyApp; Permissions: users-modify
 +</code>
 +== Resources ==
 +  * [[http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx|MSDN CSIDL]]
 +  * [[http://msdn.microsoft.com/en-us/library/dd378457(v=VS.85).aspx|MSDN KNOWNFOLDERID]]