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.
User settings are saved in a file within a subfolder of the user's local hidden application data folder.
Different types of Configuration files This file is typically found in the C:\WINDOWS\Microsoft.NET\Framework\v2. 0.50727\CONFIG directory. The Machine.
Try this:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
MessageBox.Show(config.FilePath);
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.
Use the ConfigurationManager
to get the Configuration
-object. The Configuration
-object has a string property FilePath
.
See: Configuration-Members
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With