前不久做了这么个功能(用户登入,到服务器端读取该用户的配置文件 然后在界面上显示) 想可能有些人会用到 把源代码贴上来,大家给提点意见 ^_^
1
/// <summary>
2
/// 获取 用户配置
3
/// </summary>
4
/// <param name="UserName"></param>
5
/// <param name="TableName"></param>
6
/// <returns></returns>
7
public List<string> GetUserLog(string UserName, string TableName)
8
{
9
string path = GetUserLogPath(UserName);
10
11
if (File.Exists(path))
12
{
13
XmlDocument xmlDoc = new XmlDocument();
14
xmlDoc.Load(path);
15
XmlNodeList tables = xmlDoc.SelectNodes("//UserLogs/table");
16
17
if (tables.Count > 0)
18
{
19
foreach (XmlNode table in tables)
20
{
21
if (table.Attributes[0].Value == TableName)
22
{
23
XmlNodeList tableSub = table.ChildNodes;
24
if (tableSub.Count > 0)
25
{
26
List<string> ColsValues = new List<string>();
27
foreach (XmlNode sub in tableSub)
28
{
29
ColsValues.Add(sub.InnerText);
30
}
31
return ColsValues;
32
}
33
else
34
return null;
35
}
36
//else
37
// return null;
38
}
39
return null;
40
}
41
else
42
return null;
43
}
44
else
45
{
46
XmlTextWriter xmlWriter = new XmlTextWriter(path, Encoding.Default);
47
xmlWriter.Formatting = Formatting.Indented;
48
xmlWriter.WriteStartDocument();
49
xmlWriter.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"\"");
50
xmlWriter.WriteStartElement("UserLogs");
51
xmlWriter.WriteEndElement();
52
xmlWriter.Close();
53
return null;
54
55
}
56
57
}
58
public void AddXmlNode(string path, XmlDocument xmlDoc, string TbName, List<string> Cols)
59
{
60
XmlNode root = xmlDoc.SelectSingleNode("UserLogs");
61
XmlElement xe1 = xmlDoc.CreateElement("table");
62
xe1.SetAttribute("name", TbName);
63
foreach (string col in Cols)
64
{
65
XmlElement xe1_sub = xmlDoc.CreateElement("col");
66
xe1_sub.InnerText = col;
67
xe1.AppendChild(xe1_sub);
68
}
69
root.AppendChild(xe1);
70
xmlDoc.Save(path);
71
}
72
/// <summary>
73
/// 用户修改配置(xml文件先删除再重写)
74
/// </summary>
75
/// <param name="Username"></param>
76
/// <param name="TableName"></param>
77
/// <param name="Cols"></param>
78
/// <returns></returns>
79
public bool UpdateUserLog(string Username, string TableName, List<string> Cols)
80
{
81
string path = GetUserLogPath(Username);
82
int i = 0;
83
if (path != "")
84
{
85
XmlDocument xmlDoc = new XmlDocument();
86
xmlDoc.Load(path);
87
XmlNodeList tables = xmlDoc.SelectNodes("//UserLogs/table");
88
if (tables.Count > 0)
89
{
90
foreach (XmlNode node in tables)
91
{
92
if (node.Attributes[0].Value == TableName)//修改原来table的配置
93
{
94
node.RemoveAll();
95
i++;
96
if (node.ParentNode != null)
97
node.ParentNode.RemoveChild(node);
98
AddXmlNode(path, xmlDoc, TableName, Cols);
99
break;
100
}
101
102
}
103
if (i == 0)
104
AddXmlNode(path, xmlDoc, TableName, Cols);
105
return true;
106
}
107
else
108
{
109
AddXmlNode(path, xmlDoc, TableName, Cols);
110
}
111
112
}
113
return false;
114
}
115
/// <summary>
116
/// 获得安装服务器上的用户配置文件路径
117
/// </summary>
118
/// <param name="UserName"></param>
119
/// <returns></returns>
120
private string GetUserLogPath(string UserName)
121
{
122
frm_Monitor Mon = new frm_Monitor();
123
string ApplicationP = Mon.AppinPath;
124
//TempPath tt = new TempPath();
125
//string ApplicationP = tt.AppinPath;
126
string[] ApplicationPath = ApplicationP.Split('\\');
127
string path = "";
128
if (ApplicationPath.Length > 0)
129
{
130
foreach (string str in ApplicationPath)
131
{
132
if (str == "App_Server")
133
break;
134
else
135
path += str + @"\";
136
}
137
if (path != "")
138
{
139
path += @"UserLog\" + UserName + ".xml";
140
141
}
142
143
}
144
return path;
145
}
/// <summary>2
/// 获取 用户配置3
/// </summary>4
/// <param name="UserName"></param>5
/// <param name="TableName"></param>6
/// <returns></returns>7
public List<string> GetUserLog(string UserName, string TableName)8
{9
string path = GetUserLogPath(UserName);10

11
if (File.Exists(path))12
{13
XmlDocument xmlDoc = new XmlDocument();14
xmlDoc.Load(path);15
XmlNodeList tables = xmlDoc.SelectNodes("//UserLogs/table");16

17
if (tables.Count > 0)18
{19
foreach (XmlNode table in tables)20
{21
if (table.Attributes[0].Value == TableName)22
{23
XmlNodeList tableSub = table.ChildNodes;24
if (tableSub.Count > 0)25
{26
List<string> ColsValues = new List<string>();27
foreach (XmlNode sub in tableSub)28
{29
ColsValues.Add(sub.InnerText);30
}31
return ColsValues;32
}33
else34
return null;35
}36
//else37
// return null;38
}39
return null;40
}41
else42
return null;43
}44
else45
{46
XmlTextWriter xmlWriter = new XmlTextWriter(path, Encoding.Default);47
xmlWriter.Formatting = Formatting.Indented;48
xmlWriter.WriteStartDocument();49
xmlWriter.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"\"");50
xmlWriter.WriteStartElement("UserLogs");51
xmlWriter.WriteEndElement();52
xmlWriter.Close();53
return null;54

55
}56

57
}58
public void AddXmlNode(string path, XmlDocument xmlDoc, string TbName, List<string> Cols)59
{60
XmlNode root = xmlDoc.SelectSingleNode("UserLogs");61
XmlElement xe1 = xmlDoc.CreateElement("table");62
xe1.SetAttribute("name", TbName);63
foreach (string col in Cols)64
{65
XmlElement xe1_sub = xmlDoc.CreateElement("col");66
xe1_sub.InnerText = col;67
xe1.AppendChild(xe1_sub);68
}69
root.AppendChild(xe1);70
xmlDoc.Save(path);71
}72
/// <summary>73
/// 用户修改配置(xml文件先删除再重写)74
/// </summary>75
/// <param name="Username"></param>76
/// <param name="TableName"></param>77
/// <param name="Cols"></param>78
/// <returns></returns>79
public bool UpdateUserLog(string Username, string TableName, List<string> Cols)80
{81
string path = GetUserLogPath(Username);82
int i = 0;83
if (path != "")84
{85
XmlDocument xmlDoc = new XmlDocument();86
xmlDoc.Load(path);87
XmlNodeList tables = xmlDoc.SelectNodes("//UserLogs/table");88
if (tables.Count > 0)89
{90
foreach (XmlNode node in tables)91
{92
if (node.Attributes[0].Value == TableName)//修改原来table的配置93
{94
node.RemoveAll();95
i++;96
if (node.ParentNode != null)97
node.ParentNode.RemoveChild(node);98
AddXmlNode(path, xmlDoc, TableName, Cols);99
break;100
}101

102
}103
if (i == 0)104
AddXmlNode(path, xmlDoc, TableName, Cols);105
return true;106
}107
else108
{109
AddXmlNode(path, xmlDoc, TableName, Cols);110
}111

112
}113
return false;114
}115
/// <summary>116
/// 获得安装服务器上的用户配置文件路径117
/// </summary>118
/// <param name="UserName"></param>119
/// <returns></returns>120
private string GetUserLogPath(string UserName)121
{122
frm_Monitor Mon = new frm_Monitor();123
string ApplicationP = Mon.AppinPath;124
//TempPath tt = new TempPath();125
//string ApplicationP = tt.AppinPath;126
string[] ApplicationPath = ApplicationP.Split('\\');127
string path = "";128
if (ApplicationPath.Length > 0)129
{130
foreach (string str in ApplicationPath)131
{132
if (str == "App_Server")133
break;134
else135
path += str + @"\";136
}137
if (path != "")138
{139
path += @"UserLog\" + UserName + ".xml";140

141
}142

143
}144
return path;145
}我做的主要是针对用户查询是显示数据字段的配置,大家可以根据自己的需求修改


浙公网安备 33010602011771号