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:43]
smayr
swdev:dotnet:data_and_configuration_folders [2011/04/01 16:08] (current)
smayr [InnoSetup]
Line 88: Line 88:
  
 ===== 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 97: Line 98:
                                     NULL, &wszPath )))                                     NULL, &wszPath )))
 { {
-    printf("\nSHGetKnownFolderPath FOLDERID_RoamingAppData    =%S\n", wszPath);+    printf("\nSHGetKnownFolderPath FOLDERID_RoamingAppData    = %S\n", wszPath);
 } }
  
Line 105: Line 106:
                                     NULL, &wszPath )))                                     NULL, &wszPath )))
  
-    printf("SHGetKnownFolderPath FOLDERID_Documents        =%S\n", wszPath);+    printf("SHGetKnownFolderPath FOLDERID_Documents        = %S\n", wszPath);
 } }
 </code> </code>
Line 111: 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>
  
Line 143: Line 144:
  
 ===== 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 154: Line 156:
                              szPath)))                               szPath))) 
  
-    printf("\nSHGetFolderPath CSIDL_APPDATA    =%S\n", szPath); +    printf("\nSHGetFolderPath CSIDL_APPDATA    = %S\n", szPath); 
 } }
  
Line 164: Line 166:
                              szPath)))                               szPath))) 
  
-    printf("\nSHGetFolderPath CSIDL_MYDOCUMENTS    =%S\n", szPath); +    printf("\nSHGetFolderPath CSIDL_MYDOCUMENTS    = %S\n", szPath); 
 } }
 </code> </code>
Line 170: 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 186: 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 196: Line 198:
 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 == == Resources ==
   * [[http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx|MSDN CSIDL]]   * [[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]]   * [[http://msdn.microsoft.com/en-us/library/dd378457(v=VS.85).aspx|MSDN KNOWNFOLDERID]]