简体   繁体   English

将更改保存到C#中的App.config

[英]Save Changes to App.config in C#

I Created a windows application that uses app.confing and updates app.config when needed. 我创建了一个Windows应用程序,该应用程序使用app.confing并在需要时更新app.config When I run the application in debug mode it's working fine, but after I created the .exe file and installed it's not updating the app.confing file anymore. 当我在调试模式下运行该应用程序时,它可以正常工作,但是在创建.exe文件并安装后,它不再更新app.confing文件。

I unchecked the Enable the Visual Studio hosting process to no luck. 我取消选中“启用Visual Studio托管过程不走运”。

This is the code I'm trying: 这是我正在尝试的代码:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//Updating the config values here 
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

Try calling this overload; 尝试调用此重载;

public void Save(
    ConfigurationSaveMode saveMode,
    bool forceSaveAll
)

passing forceSaveAll=true ? 通过forceSaveAll=true

Check if the app config is being hosted in the Program Files folder (or child folder) once installed. 安装后,检查应用程序配置是否托管在Program Files文件夹(或子文件夹)中。 Since in debug mode, you're running from a location in which you have write permissions, and since once installed, application config (not user config) is often sitting in Program Files, you typically won't have write permissions. 由于在调试模式下,您从具有写权限的位置运行,并且由于安装后,应用程序配置(而非用户配置)通常位于程序文件中,因此通常没有写权限。 You might try running the application as an admin to see if it works in that mode, or just go examine that directory and check the write bits for your login (assuming app is running as you). 您可以尝试以管理员身份运行该应用程序,以查看它是否可以在该模式下运行,或者只是去检查该目录并检查登录的写入位(假设应用程序正在按您的方式运行)。 Try to modify that file manually. 尝试手动修改该文件。 If you can't, that's your problem. 如果您做不到,那就是您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM