C# 删除App.config中的,WinForm 自动添加的runtime
/// <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
C#winform 程序,代码修改app.config
在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页面 中"设置"选项卡的"同步"按钮会提示这个路径。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现