The blog has moved to a new address. The blog is now located at http://devintelligence.com

Friday, August 11, 2006

How can I get the user.config file path programmatically in a non-Clickonce app?


If you want to get to the path programmatically, you can do it using the Configuration Management API. For example, here is how you can get the local user.config file path:


Note: You need to add a reference to System.Configuration.dll


Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal);
Console.WriteLine("Local user config path: {0}", config.FilePath);




Technorati : , ,

4 comments:

Anonymous said...

Is there a way to get the file path that does not require the user.config file to be valid? I want to, in the case of an invalid user.config, catch the exception and delete the corrupted file.

Attempting to use this method just throws a similar exception when the user.config file is corrupt.

Taras said...

You may use the method OpenExeConfiguration that uses ExeConfigurationFileMap object.In ExeConfigurationFileMap object you should use different configuration file mapping and you will be able
to validate the file before you access it.

Anonymous said...

I eventually found out that the file path can be obtained from a ConfigurationErrorsException. So if the user settings file is corrupt, you can delete the file. I wrote an article here.

Unknown said...

Another way for getting path to user configuration file:

string path = Application.LocalUserAppDataPath

OR

strign path = System.Windows.Forms.Application.LocalUserAppDataPath