1 public class Config
2 {
3 public Config()
4 {
5 //
6 // TODO: 在此处添加构造函数逻辑
7 //
8 }
9 public static string GetValue(string AppKey)
10 {
11 try
12 {
13 string AppKeyValue;
14 AppKeyValue = System.Configuration.ConfigurationSettings.AppSettings.Get(AppKey);
15 return AppKeyValue;
16 }
17 catch(Exception ex)
18 {
19 throw ex;
20 }
21 }
22
23 //写
24 public static void SetValue(string AppKey,string AppValue)
25 {
26 //System.Configuration.ConfigurationSettings.AppSettings.Set(AppKey,AppValue);
27 XmlDocument xDoc = new XmlDocument();
28 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
29
30 XmlNode xNode;
31 XmlElement xElem1;
32 XmlElement xElem2;
33
34 xNode = xDoc.SelectSingleNode("//appSettings");
35
36 xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
37 if ( xElem1 != null ) xElem1.SetAttribute("value",AppValue);
38 else
39 {
40 xElem2 = xDoc.CreateElement("add");
41 xElem2.SetAttribute("key",AppKey);
42 xElem2.SetAttribute("value",AppValue);
43 xNode.AppendChild(xElem2);
44 }
45 xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
46 }
47
2 {
3 public Config()
4 {
5 //
6 // TODO: 在此处添加构造函数逻辑
7 //
8 }
9 public static string GetValue(string AppKey)
10 {
11 try
12 {
13 string AppKeyValue;
14 AppKeyValue = System.Configuration.ConfigurationSettings.AppSettings.Get(AppKey);
15 return AppKeyValue;
16 }
17 catch(Exception ex)
18 {
19 throw ex;
20 }
21 }
22
23 //写
24 public static void SetValue(string AppKey,string AppValue)
25 {
26 //System.Configuration.ConfigurationSettings.AppSettings.Set(AppKey,AppValue);
27 XmlDocument xDoc = new XmlDocument();
28 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
29
30 XmlNode xNode;
31 XmlElement xElem1;
32 XmlElement xElem2;
33
34 xNode = xDoc.SelectSingleNode("//appSettings");
35
36 xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
37 if ( xElem1 != null ) xElem1.SetAttribute("value",AppValue);
38 else
39 {
40 xElem2 = xDoc.CreateElement("add");
41 xElem2.SetAttribute("key",AppKey);
42 xElem2.SetAttribute("value",AppValue);
43 xNode.AppendChild(xElem2);
44 }
45 xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
46 }
47
Sample:
1 //写配置文件
2 Config.SetValue("sPdfePath",txtPdfePath.Text);
3 Config.SetValue("sPdfNewPath",txtPdfNewPath.Text);
4 Config.SetValue("sPdfPath",txtPdfPath.Text);
5 Config.SetValue("sPecFilePath",txtPecFilePath.Text );
6 Config.SetValue("iProcNum",txtProcNum.Value.ToString());
7
8
9 // 读取配置信息
10 sPdfPath = Config.GetValue("sPdfPath");
11 sPdfNewPath = Config.GetValue("sPdfNewPath");
12 sPdfePath = Config.GetValue("sPdfePath");
13 sPecFilePath = Config.GetValue("sPecFilePath");
14 bDelSrc = bool.Parse( Config.GetValue("bDelSrc"));
15 iProcNum = int.Parse(Config.GetValue("iProcNum"));
2 Config.SetValue("sPdfePath",txtPdfePath.Text);
3 Config.SetValue("sPdfNewPath",txtPdfNewPath.Text);
4 Config.SetValue("sPdfPath",txtPdfPath.Text);
5 Config.SetValue("sPecFilePath",txtPecFilePath.Text );
6 Config.SetValue("iProcNum",txtProcNum.Value.ToString());
7
8
9 // 读取配置信息
10 sPdfPath = Config.GetValue("sPdfPath");
11 sPdfNewPath = Config.GetValue("sPdfNewPath");
12 sPdfePath = Config.GetValue("sPdfePath");
13 sPecFilePath = Config.GetValue("sPecFilePath");
14 bDelSrc = bool.Parse( Config.GetValue("bDelSrc"));
15 iProcNum = int.Parse(Config.GetValue("iProcNum"));
浙公网安备 33010602011771号