为什么在保存配置时 App.config 文件不会被覆盖? [英] Why don't App.config file doesn't get overwritten when the config is saved?

查看:34
本文介绍了为什么在保存配置时 App.config 文件不会被覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码向 WPF 应用程序中的 App.config 添加一些设置.我以为这是一个简单的过程,但最终我无法将设置保存到文件中.这是代码

I have this code to add some settings to the App.config in a WPF application. I thought it was a simple procedure, but in the end I couldn't save the setting to the file. Here is the code

 var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;

settings.Add("server2", "http://someserver.com");    
 configFile.Save(ConfigurationSaveMode.Modified);
         ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);

这里是 App.config 文件

An here is the App.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
    </startup>
  <appSettings>
    <add key="server1" value="someserver.net"/>
  </appSettings>
</configuration>

可能是什么问题?

推荐答案

每次从 Visual Studio 运行应用程序都会创建一个新的 vshost.config.所以设置又是空的.

Running the application from visual studio will create a new vshost.config every time. And so the settings are empty again.

代码正确,设置也会保存在配置文件中.尝试运行 Debug/Release 文件夹中的 .exe 文件,更改将被反映.

The code is correct and settings will also be saved in config file. Try running the .exe file from Debug/Release folder, the changes will be reflected.

这篇关于为什么在保存配置时 App.config 文件不会被覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆