This article introduces a class
ApplicationData that simplifies an application's data management
According to the "Designed for Windows XP spec v2.3" you should Classify application data into the following categories:
- Per user, roaming
- Per user, non-roaming
- Per computer (non-user specific and non-roaming)"
NOTE: The three categories are named here as: User, LocalUser and Common respectively.
Knowing where to store files and registry settings for each of this categories can be complicated and require repeating code. This utility class tries to simplify this process. Furthermore, the spec requires that application data should be stored under:
[company name]\[product name]\[version]
The required information is extracted from the assembly attributes (e.g.
AssemblyCompanyAttribute), therefore it is highly recommended to fill the appropriate attributes (typically in
AssemblyInfo.cs) when using this utility.
- C:\Documents and Settings\All Users\Application Data\My Company\My Product\1.0.840.34747\Sub1\Sub2\My File.txt
- "HKEY_CURRENT_USER\Software\My Company\My Product\1.0.840.34747\Local\Sub1\Sub1"
FileInfo file = ApplicationData.LocalUser["Sub1"]["Sub2"].GetFile("file.txt");
StreamWriter writer = file.CreateText();
writer.WriteLine("This is a test of ApplicationData!");
RegistryKey key = ApplicationData.LocalUser["Sub1"]["Sub2"].GetRegistryKey();
key.SetValue("Test", "This is a test!");
Classes used in this utility library include:
System.Windows.Forms.Application which has very useful properties such as:
System.WeakReference which is used to store the references for the three static root instances (Common, LocalUser and User). By using the weak references I don't have to recreate the instances each time they are needed and they can be garbage collected if not used. (see
ApplicationData.GetAppDataByType() for implementation)
I hope this utility comes in handy. I would appreciate any comments or suggestions you may have!