简体   繁体   English

无法在本地用户目录中使用AppSettings部分创建配置文件

[英]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.

相关问题 如何模拟.config文件的appSettings部分的文件属性 - How to simulate the file attribute of a .config file's appSettings section 使用WebConfigurationManager从Web.config文件中读取appSettings部分 - Read appSettings section from Web.config file using WebConfigurationManager 加密App.Config文件的appsettings部分中的单个密钥 - Encrypt a single key in the appsettings section of App.Config file 如何读取 web.config 文件中的 appSettings 部分? - How to read appSettings section in the web.config file? 从安装程序访问配置文件中的“ applicationSettings”部分(而非“ appSettings”) - Access section 'applicationSettings' (not 'appSettings') in config file from setup 在app.config文件的appSettings中设置文件目录 - Setting file directory in appSettings of an app.config file 在配置文件中创建自定义部分 - Create custom section in config file 是否可以在 App.config 文件的 appSettings 中引用主目录? - Is it possible to refer to home directory within appSettings in an App.config file? 无法为web.config创建自定义部分 - Unable to create a custom section for web.config 如何将EF6.1连接字符串从connectionStrings移到appSettings配置文件部分? - How to move EF6.1 connection string from connectionStrings to an appSettings config file section?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM