[英]Unable to create config file with AppSettings section in local user directory
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. 当配置文件位于应用程序目录中时,我可以从.config文件读取并向其写回更改。 I now want to change this because of user permissions and move my .config file to the users/xxx/local directory.
现在,由于用户权限,我想更改此设置,然后将.config文件移动到users / xxx / local目录。 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.
我已经了解到您需要将SectionInformation.AllowExeDefinition属性更改为ConfigurationAllowExeDefinition.MachineToLocalUser,但是我无法为AppSettings部分更改此属性。
This is how I read from the AppSettings 这就是我从AppSettings中读取的内容
//Read COM Port settings
UIModel_settings.COMport = System.Configuration.ConfigurationManager.AppSettings.Get("COMPort");
This is how I write to the AppSettings 这就是我写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. 当我运行时,在Save方法中出现锁定错误:锁定后无法编辑ConfigurationSection属性。
Any ideas how to fix this? 任何想法如何解决这一问题? Or how I can create this file under my local users directory?
或者如何在本地用户目录下创建此文件?
Thanks 谢谢
Zjerre Zjerre
In your Project, do you have a Settings File Item (eg Settings.settings)? 在您的项目中,您是否有一个设置文件项(例如Settings.settings)?
If so this will generate a Settings class with a static Default property that you can use to access the appSettings elements. 如果是这样,这将生成一个带有静态Default属性的Settings类,您可以使用该属性访问appSettings元素。 If not then you could try adding one from the Solution Explorer -> Project -> Add Item -> Settings File.
如果没有,那么您可以尝试从解决方案资源管理器->项目->添加项目->设置文件中添加一个。
UIModel_settings.COMport = Settings.Default.COMPort;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.