Getting a common CommonAppDataPath

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:

Base Path\CompanyName\ProductName\ProductVersion

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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s