首页  ·  知识 ·  编程语言
app.config的节点AppSetting新增或修改操作
阿良.NET  http://www.chenjiliang.com/Article/View.aspx?Artic  .NET  编辑:dezai  图片来源:网络
//新增更改app.Config的值 private void OptConfig(string AppKey,
//新增更改app.Config的值
private void OptConfig(string AppKey, string AppValue)
{
    try
    {
        Assembly Asm = Assembly.GetExecutingAssembly();
        XmlDocument xmlDoc = new XmlDocument();

        string m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + "YourAppName.exe.config";
        xmlDoc.Load(m_strFullPath);

        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(m_strFullPath);
        XmlNode xNode;
        XmlElement xElem1;
        XmlElement xElem2;
        xNode = xDoc.SelectSingleNode("//appSettings");
        xElem1 = (XmlElement) xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
        if (xElem1 != null)
        {
            xElem1.SetAttribute("value", AppValue);
        }
        else
        {
            xElem2 = xDoc.CreateElement("add");
            xElem2.SetAttribute("key", AppKey);
            xElem2.SetAttribute("value", AppValue);
            xNode.AppendChild(xElem2);
        }
        xDoc.Save(m_strFullPath);
    }
    catch (System.NullReferenceException NullEx)
    {
        throw NullEx;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
本文作者:阿良.NET 来源:http://www.chenjiliang.com/Article/View.aspx?Artic
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读