Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:dotnet:data_and_configuration_folders [2011/04/01 09:35] smayr |
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:// | + | Source: [[http:// |
=== 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 7: | Line 7: | ||
Over the past few releases of Windows, you may have noticed common folder locations have moved around a bit. What should you do if you want your code to target multiple OS’s? Perhaps you are updating an application from XP to Windows 7 and wondering where those old directories went. Hopefully, this post will answer those questions and help you design your application to continue to work with future OS releases. | Over the past few releases of Windows, you may have noticed common folder locations have moved around a bit. What should you do if you want your code to target multiple OS’s? Perhaps you are updating an application from XP to Windows 7 and wondering where those old directories went. Hopefully, this post will answer those questions and help you design your application to continue to work with future OS releases. | ||
- | '' | + | ==== What’s the Recommended Location for Application Files? |
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: | ||
</ | </ | ||
- | '' | + | ===== 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: | ||
</ | </ | ||
- | '' | + | ===== 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: | ||
</ | </ | ||
- | '' | + | ===== Per user “Documents” |
“Document” type files that users create/ | “Document” type files that users create/ | ||
Line 68: | Line 69: | ||
</ | </ | ||
- | '' | + | ===== Per Machine “Documents” |
“Document” type files that users create/ | “Document” type files that users create/ | ||
Line 84: | Line 85: | ||
It’s obvious after looking at all these locations that where you store your files can be challenging if you are targeting multiple OS versions. | It’s obvious after looking at all these locations that where you store your files can be challenging if you are targeting multiple OS versions. | ||
- | '' | + | ==== 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, & | NULL, & | ||
{ | { | ||
- | printf(" | + | printf(" |
} | } | ||
Line 104: | Line 106: | ||
NULL, & | NULL, & | ||
{ | { | ||
- | printf(" | + | printf(" |
} | } | ||
</ | </ | ||
Line 110: | Line 112: | ||
The output on Windows 7 is: | The output on Windows 7 is: | ||
< | < | ||
- | SHGetKnownFolderPath FOLDERID_RoamingAppData =C: | + | SHGetKnownFolderPath FOLDERID_RoamingAppData = C: |
- | SHGetKnownFolderPath FOLDERID_Documents | + | SHGetKnownFolderPath FOLDERID_Documents |
</ | </ | ||
- | '' | + | ===== 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 139: | Line 141: | ||
Note that the path for Documents returns my redirected IntelliMirror server path because I ran it on a domain joined machine and my domain account is IntelliMirror enabled. | Note that the path for Documents returns my redirected IntelliMirror server path because I ran it on a domain joined machine and my domain account is IntelliMirror enabled. | ||
- | '' | + | ==== 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. | If you are still targeting XP, you’ll need to use the legacy API SHGetFolderPath that uses CSIDL’s. | ||
Line 153: | Line 156: | ||
| | ||
{ | { | ||
- | printf(" | + | printf(" |
} | } | ||
Line 163: | Line 166: | ||
| | ||
{ | { | ||
- | printf(" | + | printf(" |
} | } | ||
</ | </ | ||
Line 169: | Line 172: | ||
The output on Windows 7 is: | The output on Windows 7 is: | ||
< | < | ||
- | SHGetFolderPath CSIDL_APPDATA | + | SHGetFolderPath CSIDL_APPDATA |
- | SHGetFolderPath CSIDL_MYDOCUMENTS =\\zaw\Mydocs\patricka\My Documents | + | SHGetFolderPath CSIDL_MYDOCUMENTS = \\zaw\Mydocs\patricka\My Documents |
</ | </ | ||
The output on XP is: | The output on XP is: | ||
< | < | ||
- | SHGetFolderPath CSIDL_APPDATA | + | SHGetFolderPath CSIDL_APPDATA |
- | SHGetFolderPath CSIDL_MYDOCUMENTS =C: | + | SHGetFolderPath CSIDL_MYDOCUMENTS = C: |
</ | </ | ||
The API returns the appropriate path for the given OS version. Please note: I ran this in XP Mode on Windows 7. My XP Mode machine was not domain joined; therefore, I did not get IntelliMirror redirection. | The API returns the appropriate path for the given OS version. Please note: I ran this in XP Mode on Windows 7. My XP Mode machine was not domain joined; therefore, I did not get IntelliMirror redirection. | ||
- | '' | + | ===== Managed Code ===== |
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: | ||
< | < | ||
- | SpecialFolder.ApplicationData path =C: | + | SpecialFolder.ApplicationData path = C: |
- | SpecialFolder.MyDocuments path =C: | + | SpecialFolder.MyDocuments path = C: |
</ | </ | ||
Similar to the native API’s, we get the appropriate location for XP. Again, because my XP Mode virtual machine was not domain joined, there is no IntelliMirror redirection. | Similar to the native API’s, we get the appropriate location for XP. Again, because my XP Mode virtual machine was not domain joined, there is no IntelliMirror redirection. | ||
- | '' | + | ==== Final Thoughts |
Think about the type of files your application is writing. | Think about the type of files your application is writing. | ||
+ | |||
+ | == Best Practices == | ||
+ | |||
+ | Store only application files in '' | ||
+ | Store share application data in '' | ||
+ | Store user data in '' | ||
+ | |||
+ | ==== Coding in C# ==== | ||
+ | Use '' | ||
+ | <code csharp> | ||
+ | string userAppData | ||
+ | string commonAppData = Envrionment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); | ||
+ | </ | ||
+ | |||
+ | ==== InnoSetup ==== | ||
+ | Set proper permissions for shared '' | ||
+ | <code inno> | ||
+ | [Dirs] | ||
+ | Name: {commonappdata}\Acme\MyApp; | ||
+ | </ | ||
+ | == Resources == | ||
+ | * [[http:// | ||
+ | * [[http:// |