I’ve recently been working with an app that needs to save configuration data to a file, accessible to all users. At first I tried the Application.StartupPath (where the executable lives), which works fine in a debugging environment, but once the program was installed, Vista’s UAC came to the fore and refused access to the C:\Program Files\ directory. Fair enough, but I didn’t want to use My Documents. Fortunately, the Application class has another static property, Application.CommonAppDataPath.
The problem I have with CommonAppDataPath is that the folder it supplies is assembly version dependant, that is, the returned path is of the form:
The issue here is that, if the assembly is updated (a new ProductVersion), the path changes and the configuration file is no longer found. The solution is to strip the last folder off, as shown below.
string configPath = Application.CommonAppDataPath; configPath = configPath.Substring(0, configPath.LastIndexOf('\\'));
The returned path (like the original) does not have a trailing ‘\’, so remember to add this along with your config file name.