Reputation: 105
I'm able to read from a .config file and write changes back to it when the config file is located in the application directory. I now want to change this because of user permissions and move my .config file to the users/xxx/local directory. I've already learned you need to change the SectionInformation.AllowExeDefinition property to ConfigurationAllowExeDefinition.MachineToLocalUser But I'm unable to change this property for the AppSettings Section.
This is how I read from the AppSettings
//Read COM Port settings
UIModel_settings.COMport = System.Configuration.ConfigurationManager.AppSettings.Get("COMPort");
This is how I write to the AppSettings
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
config.AppSettings.Settings["COMPort"].Value = UIModel_settings.COMport;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
When I run I get the locked error at the Save method: ConfigurationSection properties cannot be edited when locked.
Any ideas how to fix this? Or how I can create this file under my local users directory?
Thanks
Zjerre
Upvotes: 1
Views: 810
Reputation: 36
In your Project, do you have a Settings File Item (e.g. Settings.settings)?
If so this will generate a Settings class with a static Default property that you can use to access the appSettings elements. If not then you could try adding one from the Solution Explorer -> Project -> Add Item -> Settings File.
UIModel_settings.COMport = Settings.Default.COMPort;
Upvotes: 2