Jcstone / C#开发 / C# Winform 修改app.config文件

0 0

   

C# Winform 修改app.config文件

2013-03-02  Jcstone

C# Winform 修改app.config文件  

2011-09-13 15:13:14|  分类: C# |字号 订阅

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings></connectionStrings>
    <appSettings>
        <add key="Server" value=" "/>
        <add key="initial catalog" value=" "></add>
        <add key="uid" value=" "></add>
        <add key="pwd" value=" "></add>
    </appSettings>
</configuration>
#region  方法保存修改的设置
        /// <summary>
        /// 方法保存修改的设置
        /// </summary>
        /// <param name="ConnenctionString"></param>
        /// <param name="strKey"></param>
        private void SaveConfig(string ConnenctionString, string strKey)
        {
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                if (att.Value == strKey)
                {
                    //对目标元素中的第二个属性赋值
                    att = nodes[i].Attributes["value"];
                    att.Value = ConnenctionString;
                    break;
                }
            }
            //保存上面的修改
            doc.Save(strFileName);
        }
        #endregion
//调用方法修改app.config文件
private void btnOK_Click_1(object sender, EventArgs e)
{
       SaveConfig("WWW-8CB7FB23DD4","Server");
       SaveConfig("myehrce","initial catalog");
       SaveConfig("sa", "uid");
       SaveConfig("123","pwd");
}

c# 操作配置文件app.config
在c#出现以前,应用程序通常都是采用ini或xml作为配置文件,但c#出现后,大部分程序员还是选择用可操作性强的.config应用程序配置文件的。所以,为了操作方便,通常需要写一个通用类。网上有很多这样的通用类,但我觉得我写的这个通用辅助类对于我来说很方便,看各人喜好喽。

using system;
using system.collections.generic;
using system.text;
using system.configuration;

namespace schwann.commlibrary
{
    public class confighelper
    {
        /// <summary>
        /// 根据键值获取配置文件
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public static string getconfig(string key)
        {
            string val = string.empty;
            if (configurationmanager.apps教程ettings.allkeys.contains(key))
                val = configurationmanager.appsettings[key];
            return val;
        }

        /// <summary>
        /// 获取所有配置文件
        /// </summary>
        /// <returns></returns>
        public static dictionary<string, string> getconfig()
        {
            dictionary<string, string> dict = new dictionary<string, string>();
            foreach (string key in configurationmanager.appsettings.allkeys)
                dict.add(key, configurationmanager.appsettings[key]);
            return dict;
        }

        /// <summary>
        /// 根据键值获取配置文件
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static string getconfig(string key, string defaultvalue)
        {
            string val = defaultvalue;
            if (configurationmanager.appsettings.allkeys.contains(key))
                val = configurationmanager.appsettings[key];
            if (val == null)
                val = defaultvalue;
            return val;
        }

        /// <summary>
        /// 写配置文件,如果节点不存在则自动创建
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public static bool setconfig(string key, string value)
        {
            try
            {
                configuration conf = configurationmanager.openexeconfiguration(configurationuserlevel.none);
                if (!conf.appsettings.settings.allkeys.contains(key))
                    conf.appsettings.settings.add(key, value);
                else
                    conf.appsettings.settings[key].value = value;
                conf.save();
                return true;
            }
            catch { return false; }
        }

        /// <summary>
        /// 写配置文件(用键值创建),如果节点不存在则自动创建
        /// </summary>
        /// <param name="dict">键值集合</param>
        /// <returns></returns>
        public static bool setconfig(dictionary<string, string> dict)
        {
            try
            {
                if (dict == null || dict.count == 0)
                    return false;
                configuration conf = configurationmanager.openexeconfiguration(configurationuserlevel.none);
                foreach (string key in dict.keys)
                {
                    if (!conf.appsettings.settings.allkeys.contains(key))
                        conf.appsettings.settings.add(key, dict[key]);
                    else
                        conf.appsettings.settings[key].value = dict[key];
                }
                conf.save();
                return true;
            }
            catch { return false; }
        }
    }
}

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。如发现有害或侵权内容,请点击这里 或 拨打24小时举报电话:4000070609 与我们联系。

    猜你喜欢

    0条评论

    发表

    请遵守用户 评论公约

    类似文章
    喜欢该文的人也喜欢 更多