MachineConfiguration.cs
4.4 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
using System;
using System.IO;
using System.Xml;
using System.Windows.Forms;
namespace ServiceManager
{
/// <summary>
/// MachineConfiguration 的摘要说明。
/// </summary>
public class MachineConfiguration
{
private string _MachineConfigPath;
public const string SYSTEMWEB = @"system.web";
public MachineConfiguration()
{
Initialize();
}
/// <summary>
/// 初始化
/// </summary>
public void Initialize()
{
string f_SystemFolder = "";
try
{
f_SystemFolder = Environment.SystemDirectory;
f_SystemFolder = f_SystemFolder.Substring(0,f_SystemFolder.LastIndexOf(@"\"));
f_SystemFolder += @"\Microsoft.NET\Framework\v";
f_SystemFolder += Environment.Version.ToString();
f_SystemFolder = f_SystemFolder.Substring(0,f_SystemFolder.LastIndexOf(@"."));
f_SystemFolder += @"\Config\machine.config";
}
catch
{
_MachineConfigPath = "";
}
_MachineConfigPath = f_SystemFolder;
}
/// <summary>
/// 设置<configuration/><system.web/><processModel/>节点userName属性设为"System"
/// </summary>
/// <returns>操作成功 返回true,操作失败 返回false</returns>
public bool ProcessModelSetUserName()
{
string f_NodeName = "processModel";
string f_NodeKey = "userName";
string f_NodeValue = "System";
if (ModifyConfigSetting(MachineConfiguration.SYSTEMWEB, f_NodeName, f_NodeKey, f_NodeValue))
return true;
else
return false;
}
/// <summary>
/// 实现对machine节点的属性值修改的封装
/// </summary>
/// <param name="ConfigNode">设置文件主节点</param>
/// <param name="NodeName">设置节点</param>
/// <param name="NodeKey">键标识</param>
/// <param name="NodeValue">键值</param>
/// <returns>操作成功 返回true,操作失败 返回false</returns>
public bool ModifyConfigSetting(string ConfigNode,string NodeName,string NodeKey,string NodeValue)
{
if(!File.Exists(_MachineConfigPath))
{
return false;
}
XmlDocument doc = new XmlDocument();
try
{
//确保 config 文件可写 Benjamin Qin 2003-11-13
FileAttributes m_fa = File.GetAttributes(_MachineConfigPath);
if((m_fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) //若为只读
File.SetAttributes(_MachineConfigPath,m_fa^FileAttributes.ReadOnly);//则设为非只读
if((m_fa & FileAttributes.Hidden) == FileAttributes.Hidden) //若为隐藏
File.SetAttributes(_MachineConfigPath,m_fa^FileAttributes.Hidden);//则设为非隐藏
doc.Load(_MachineConfigPath);
}
catch
{
return false;
}
XmlNode node = doc.DocumentElement;
string f_xPath = "";
try
{
f_xPath = "descendant::" + ConfigNode;
//node = node.SelectSingleNode(f_xPath);
//f_xPath = ConfigNode;//"descendant::" + ;
//node = node.SelectSingleNode(f_xPath);
if (node == null)
{
return false;
}
else
{
f_xPath = "descendant::" + NodeName;
node = node.SelectSingleNode(f_xPath);
if (node == null)
{
return false;
}
else
{
try
{
//node.Attributes[NodeKey].Value = NodeValue;
}
catch
{
//MessageBox.Show("7" + ConfigNode.ToString() + " " + NodeKey.ToString() + " " + NodeName.ToString() + " " + NodeValue.ToString());
return false;
}
}
}
try
{
doc.Save(_MachineConfigPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
return true;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
}
/// <summary>
/// Machine.config路径
/// </summary>
public string MachineConfigPath
{
get
{
return _MachineConfigPath;
}
set
{
_MachineConfigPath = value;
}
}
}
}