Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the location of the user.config file in programmatically?

I'd like to display the location of the user.config file in my windows forms application so a user can easily find it.

I understand how the path is created thanks to: Can I control the location of .NET user settings to avoid losing settings on application upgrade?.

However, in case this changes, I would rather not have to construct path this in my app, especially if there is an easy method for getting the user.config file location.

like image 889
Chris Weber Avatar asked Jan 27 '12 18:01

Chris Weber


People also ask

Where are C# user settings stored?

User settings are saved in a file within a subfolder of the user's local hidden application data folder.

Where is the .NET config file?

Different types of Configuration files This file is typically found in the C:\WINDOWS\Microsoft.NET\Framework\v2. 0.50727\CONFIG directory. The Machine.


3 Answers

Try this:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);

MessageBox.Show(config.FilePath);
like image 61
Francis Avatar answered Oct 08 '22 11:10

Francis


Depending on how your application runs, ConfigurationUserLevel.PerUserRoamingAndLocal may be the property you're looking for rather than ConfigurationUserLevel.PerUserRoaming;

i.e:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
MessageBox.Show(config.FilePath);

Be sure to have System.Configuration in your project's references in order to use this.

like image 43
c0g Avatar answered Oct 08 '22 11:10

c0g


Use the ConfigurationManager to get the Configuration-object. The Configuration-object has a string property FilePath. See: Configuration-Members

like image 2
Mithrandir Avatar answered Oct 08 '22 11:10

Mithrandir