zoukankan
html css js c++ java
动态编辑程序配置文件
Code
1
using
System;
2
using
System.Collections.Generic;
3
using
System.ComponentModel;
4
using
System.Data;
5
using
System.Drawing;
6
using
System.Linq;
7
using
System.Text;
8
using
System.Windows.Forms;
9
using
System.Xml;
10
11
namespace
WindowsFormsApplication1
12
{
13
public
partial
class
Form1 : Form
14
{
15
public
Form1()
16
{
17
InitializeComponent();
18
}
19
20
private
void
button1_Click(
object
sender, EventArgs e)
21
{
22
SaveConfig(
"
welcome
"
);
23
}
24
/**/
///
<summary>
25
///
设置配置文件
26
///
</summary>
27
///
<param name="ConnenctionString"></param>
28
private
void
SaveConfig(
string
ConnenctionString)
29
{
30
XmlDocument doc
=
new
XmlDocument();
31
//
获得配置文件的全路径
32
string
strFileName
=
AppDomain.CurrentDomain.BaseDirectory.ToString()
+
"
WindowsFormsApplication1.exe.config
"
;
33
doc.Load(strFileName);
34
//
找出名称为“add”的所有元素
35
XmlNodeList nodes
=
doc.GetElementsByTagName(
"
add
"
);
36
/**/
///
这里修改原结点
37
for
(
int
i
=
0
; i
<
nodes.Count; i
++
)
38
{
39
//
获得将当前元素的key属性
40
XmlAttribute att
=
nodes[i].Attributes[
"
name
"
];
41
42
//
根据元素的第一个属性来判断当前的元素是不是目标元素
43
if
(att.Value
==
"
my
"
)
44
{
45
//
对目标元素中的第二个属性赋值
46
att
=
nodes[i].Attributes[
"
connectionString
"
];
47
att.Value
=
ConnenctionString;
48
break
;
49
}
50
}
51
52
/**/
///
这里新加结点
53
nodes
=
doc.GetElementsByTagName(
"
connectionStrings
"
);
54
for
(
int
i
=
0
; i
<
nodes.Count; i
++
)
55
{
56
XmlNode nd
=
nodes[i];
57
XmlNode cnd
=
null
;
58
if
(nd.HasChildNodes)
59
{
60
cnd
=
nd.ChildNodes[i].Clone();
61
cnd.Attributes[
"
name
"
].Value
=
"
newvalue
"
;
62
cnd.Attributes[
"
connectionString
"
].Value
=
"
heelo!
"
;
63
}
64
65
66
nd.AppendChild(cnd);
67
68
}
69
//
保存上面的修改
70
doc.Save(strFileName);
71
}
72
73
}
74
}
75
这里和大家分享和学习如何学IT!
查看全文
相关阅读:
java 多线程小记
Java通过反射实现实例化
selenium webdriver 屏幕滚动
selenium webdriver 实现百度贴吧自动签到
selenium webdriver 小计
JaveWeb 公司项目(7)----- 通过JS动态生成DIV
两个DIV并排显示
Intellij idea注册码失效
JaveWeb 公司项目(6)----- 通过ToolTip给控件添加动态注释
Intellij idea 添加浏览器
原文地址:https://www.cnblogs.com/fuchifeng/p/1268275.html
最新文章
dtoj#4211. 一排数(array)
dtoj#4212. 小X爱旅行(travel)
dtoj#4194. 「JOI 2019 Final」有趣的家庭菜园 3
bzoj4919: [Lydsy1706月赛]大根堆
bzoj3674: 可持久化并查集加强版
CCF南京预告:钟山论坛——开源国产数据库与产学研联盟创新
开源国产数据库峰会-南京站(8月15日)
PostgreSQL公开课(第19期)-Toast技术特点与应用
CUUG的Oracle OCP 19c的题库解析(免费报名参与)
PostgreSQL免费公开课第17期-full-page写操作机制
热门文章
PostgreSQL免费公开课第18期-控制文件管理与恢复
PostgreSQL免费公开课第16期-(TXIDS内核)
PostgreSQL免费公开课第15期-调优系列课程(pgBagder)
PostgreSQL 免费公开课(第14期)(CUUG)(2020年)
PostgreSQL 免费公开课(第13期)(CUUG)(2020年)
函数定义
函数作用域和声明提前
linux常用命令全集
代理配置教程
测试相关理论
Copyright © 2011-2022 走看看