将键值对写入 app.config 文件 [英] Writing key value pairs into app.config file

查看:40
本文介绍了将键值对写入 app.config 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它与 Read 一起工作得很好,但我似乎无法让 Write 发生.当我尝试写入文件时,它不会抛出任何 IO 异常.好像什么都没发生.

It works fine with Read, but I can't seem to get the Write happening. It doesn't throw any IO Exception when I try to write file. As if nothing happened.

这是我的代码,请看GetValue()SetValue()函数:

Here is my code, please look at the GetValue() and SetValue() functions:

using System.Configuration;

public class AppConfig {
    private string _username;
    private string _password; 

    public AppConfig() {
        _filePath = GetValue("Username");
        _password = GetValue("Password");
        //... more
    }
    public string Password {
        get { return _password; }
        set { SetValue("Password", value); _password = value; }
    }
    public string Username {
        get { return _username; }
        set { SetValue("Username", value); _username = value; }
    }
    private void SetValue(string key, string val) {
        var cfg= ConfigurationManager
                .OpenExeConfiguration(ConfigurationUserLevel.None);
        cfg.AppSettings.Settings[key].Value = val;
        cfg.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }
    private string GetValue(string key) {
        return ConfigurationManager.AppSettings[key];
    }
}

这是 app.config 文件:

And this is the app.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>

    <add key ="Password" value ="123456"/>
    <add key ="Username" value ="hohoho"/>

  </appSettings>
</configuration>

知道如何使 Write 发生吗?谢谢.

Any idea how to make the Write happening? Thank you.

推荐答案

.Net 中的配置系统并非旨在让您以编程方式将更改保存到程序集文件夹中的 app.config.我知道那里有一个保存"方法,但这些更改实际上保存到配置文件的副本中.副本的位置取决于设置的范围.

The configuration system in .Net was not designed to let you programmatically save changes to the app.config in the assembly folder. I know there is a "Save" method there, but those changes actually save to a copy of the config file. The location of the copy depends on the scope of the setting.

应用程序设置,例如:

<appSettings>
     <add key="" value=""/>
</appSettings>

如果我没记错的话,有应用范围".如果您使用项目设置页面中的设置页面或打开属性文件夹中的Settings.settings,您可以选择范围(用户或应用程序).

Have "application scope" if I recall correctly. If you use the Settings page in the project settings page or open Settings.settings in the Properties folder, you can choose the scope (User or Application).

这样,对当前用户或该特定版本的应用程序的设置更改将被保留.这些副本存储在相应帐户的 %APPDATA% 中某个生成的文件夹中.配置系统会根据登录用户自动重新加载设置.

This way, changes to the settings are persisted for the current user or for that specific version of the application. These copies are stored in a generated folder somewhere in the %APPDATA% for the appropriate account. The configuration system automatically loads the settings again depending on who's logged in.

这就是为什么您还有升级"方法.它允许您将设置添加到应用程序的新版本并升级用户的设置,从而仅将新属性添加到用户的副本中.这样,程序集文件夹中app.config中的设置只是设置的默认值.

This is why you also have an "Upgrade" method. It allows you to add settings to new versions of your application and upgrade the user's settings, causing only the new properties to be added to the user's copy. In this way, the setting in the app.config in the assembly folder is only the default value for the setting.

上面的评论说明了为什么会这样:Program Files 中的文件旨在在安装后保持原样,而 UAC 确保这一点.

The comments above touch on why this is the way it works: Files inside Program Files are intended to remain untouched after installation and UAC ensures this.

我建议阅读配置系统,它非常强大.如果你还是真的要修改app.config,你必须写自定义代码直接修改app.config文件

I suggest reading up on the configuration system, it is quite powerful. If you still really want to modify app.config, you have to write custom code to modify the app.config file directly and

  • 安装到用户的配置文件文件夹中,或
  • 始终以以下方式运行您的程序管理员

这篇关于将键值对写入 app.config 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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