WebConfig.cs
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.IO;
using System.Xml;
namespace App
{
/// <summary>
/// WebConfig 的摘要说明。
/// </summary>
public class WebConfig
{
private string _WebConfigPath = @"E:\MyCMMI\MyCMMI\WebService\";
// private string _WebServiceUrl=@"http://127.0.0.1/WebService/";
/// <summary>
/// 用户节点名字
/// </summary>
public const string ApplicationConfiguration="ApplicationConfiguration";
public const string MyCMMIConfiguration = "MyCMMIConfiguration";
public WebConfig()
{
Initialize();
}
private void Initialize()
{
}
/// <summary>
/// 动态设置Web.Config
/// </summary>
/// <param name="UseConfigNode">用户节点名</param>
/// <param name="AddKey">键标识</param>
/// <param name="AddValue">键值</param>
/// <returns>True为成功,False为不成功</returns>
public bool SetWebConfig(string UseConfigNode,string AddKey,string AddValue)
{
XmlDocument doc = new XmlDocument();
try
{
//确保 config 文件可写 Benjamin Qin 2003-11-13
FileAttributes m_fa = File.GetAttributes(_WebConfigPath);
if((m_fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) //若为只读
File.SetAttributes(_WebConfigPath,m_fa^FileAttributes.ReadOnly);//则设为非只读
if((m_fa & FileAttributes.Hidden) == FileAttributes.Hidden) //若为隐藏
File.SetAttributes(_WebConfigPath,m_fa^FileAttributes.Hidden);//则设为非隐藏
doc.Load(_WebConfigPath);
}
catch
{
return false;
}
XmlNode node=doc.DocumentElement;
string xpath = "descendant::" + UseConfigNode;//string xpath = "descendant::" + UseConfigNode;
node=node.SelectSingleNode(xpath);
if(node==null)
{
return false;
}
else
{
xpath="add[@key='" + AddKey + "']";
node=node.SelectSingleNode(xpath);
if(node==null)
{
node=doc.DocumentElement;
xpath="descendant::"+UseConfigNode;
node=node.SelectSingleNode(xpath);
XmlElement elem = doc.CreateElement("add");
elem.SetAttribute("key",AddKey);
elem.SetAttribute("value",AddValue);
node.AppendChild(elem);
try
{
doc.Save(_WebConfigPath);
}
catch
{
return false;
}
return true;
}
else
{
node.Attributes["value"].Value=AddValue;
try
{
doc.Save(_WebConfigPath);
}
catch
{
return false;
}
return true;
}
}
}
/// <summary>
/// 获取Web.Config中用户属性值
/// </summary>
/// <param name="UseConfigNode">用户节点名</param>
/// <param name="AddKey">键标识</param>
/// <returns>返回键值,如找不到等其他情况返回空</returns>
public string GetWebConfig(string UseConfigNode,string AddKey)
{
XmlDocument doc = new XmlDocument();
try
{
doc.Load(_WebConfigPath);
}
catch
{
return "";
}
XmlNode node=doc.DocumentElement;
string xpath="descendant::" + UseConfigNode;
node=node.SelectSingleNode(xpath);
if(node==null)
{
return "";
}
xpath="descendant::add[@key='" + AddKey + "']";
node=node.SelectSingleNode(xpath);
if(node!=null)
{
return node.Attributes["value"].Value;
}
else
{
return "";
}
}
// public string WebServiceUrl
// {
// get
// {
// return _WebServiceUrl;
// }
// set
// {
// _WebServiceUrl=value;
// }
// }
/// <summary>
/// Config 文件的绝对路径,应包含文件名
/// </summary>
public string WebConfigPath
{
get
{
return _WebConfigPath;
}
set
{
_WebConfigPath=value;
}
}
}
}