winForm修改怎样保存后的app.Config 文件?

来源:百度知道 编辑:UC知道 时间:2024/06/30 11:26:19
我在Winform项目中新建了一个配置文件 App.config

在里面设置了一个参数判断 复选框是否选中,
选中就吧参数的值改为1,没选中就是0

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!--启用充值赠送 0为不启用-->
<add key="isPresent" value="0"/>
</appSettings>
</configuration>

这是我写的获取方法,和修改方法,但是都不能修改后写进配置中

public static class ConfigClass
{
public static string GetValue(string appKey)
{
return ConfigurationManager.AppSettings[appKey];
}

public static void SetValue(string appKey, string appValue)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");

XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;

用xml操作

最好不要用直接访问XML的方式操作配置文件,微软已经提供了配置文件的API,推荐的方法:
Configuration configuration = ConfigurationManager.OpenExeConfiguration(filePath);
AppSettingsSection appSection = configuration.AppSettings;
appSection.Settings[key].Value = value;
configuration.Save();

详见:http://msdn.microsoft.com/zh-cn/library/system.configuration.configurationmanager.openexeconfiguration.aspx

调试下,xml这东西要慢慢调。

清理项目以后是不是连配置文件一块儿清除了啊,再生成项目以后又恢复原来默认的0了