• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#删除App.config中的,WinForm自动添加的runtimeC#winform程序,代码修改app.config ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 /// <summary>
        /// 清空App.config节点下的内容
        /// </summary>
        /// <param name="strNode"></param>
        public static void AppConfigRemoveChild(string strNode)
        {
            XmlDocument xDoc = new XmlDocument();
            //获取可执行文件的路径和名称
            xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
            using (XmlNodeList xnl = xDoc.SelectSingleNode("//"+ strNode).ChildNodes) 
            {
                for (int i = 0; i < xnl.Count; i++)
                {
                    xnl[i].ParentNode.RemoveChild(xnl[i]);
                }
                xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
            }
        }

 

 

这是参考文章

https://blog.csdn.net/chunleixiahe/article/details/55504152

 

1、test.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<TestList>
  <test a="" b=""/>

 <test a="" b=""/>
</TestList>

2、实现代码

           bool bSuccess = true;
            while (bSuccess)
            {
                string strTaskListPath = CommVar.curExecPath + "test.xml";
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(strTaskListPath);
                XmlNodeList xnl = xmlDoc.SelectSingleNode("TestList").ChildNodes;
                int nCount = xnl.Count;
                if (0 == nCount)
                {
                    bSuccess = false;
                }
                for (int i = 0; i < xnl.Count; i++)
                {
                    xnl[i].ParentNode.RemoveChild(xnl[i]);
                }
                xmlDoc.Save(strTaskListPath);
            }

3、实现结果

<?xml version="1.0" encoding="UTF-8"?>
<TestList>

</TestList>
————————————————
版权声明:本文为CSDN博主「春蕾夏荷_728297725」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/chunleixiahe/article/details/55504152

 

 

 

 

 

 

参考2

 

 

https://www.cnblogs.com/top5/archive/2010/02/19/1669520.html

}

 

当Properties.Settings变量的范围"scope"设置为用户"user"时,通过上述方式读写操作并不是操作 了"test.exe.config"文件,实际操作的文件保存在"C:\Documents and Settings\Administrator\Local Settings\Application Data\"路径下面(注:Administrator是当前用户文件夹),文件名字叫"user.config"。点击工程Properties页面 中"设置"选项卡的"同步"按钮会提示这个路径。 

 
posted @ 2010-02-19 22:23  与时俱进  阅读(3176)  评论(0)  编辑  收藏  举报

 

用下面的方法可以操作应用程序文件夹下的配置文件:

在winform中使用程序读取和修改App.config里面的appSettings当中的Value值

这里我写成了两个方法,以供大家参考!
一,命名空间
using System;
using System.Configuration;
using System.Xml;
二,方法
//读取Value值
public static string GetConfigString(string key)
{
//
// TODO: 在此处添加构造函数逻辑
//
return ConfigurationSettings.AppSettings[key];
}
//写操作
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;
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(System.Windows.Forms.Application.ExecutablePath + ".config");

}

 

当Properties.Settings变量的范围"scope"设置为用户"user"时,通过上述方式读写操作并不是操作 了"test.exe.config"文件,实际操作的文件保存在"C:\Documents and Settings\Administrator\Local Settings\Application Data\"路径下面(注:Administrator是当前用户文件夹),文件名字叫"user.config"。点击工程Properties页面 中"设置"选项卡的"同步"按钮会提示这个路径。 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap