-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectConf.cs
140 lines (135 loc) · 5.21 KB
/
ConnectConf.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CodeCreator
{
public class ConnectConf
{
public ConnectConf()
{
ReadConf();
}
public ConntectItem DefaultItem { set; get; }
public List<ConntectItem> Items { set; get; }
public void ReadConf()
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "conn.conf");
if (!File.Exists(path)) { DefaultItem = null; Items = new List<ConntectItem>(); }
Items = new List<ConntectItem>();
ConntectItem tmp = null;
string[] lines = File.ReadAllLines(path);
bool flag = false;
foreach (var item in lines)
{
if (item == "#Item_Start")
{
tmp = new ConntectItem();
Items.Add(tmp);
flag = true;
}
else if (item == "#Item_End")
{
flag = false;
}
else
{
if (!flag) { continue; }
string str = item.Trim('\t').Trim();
if (str.StartsWith("#ItemID"))
{
str = str.Substring("#ItemID".Length).Trim().TrimStart('=');
tmp.ItemID = str;
}
else if (str.StartsWith("#ItemName"))
{
str = str.Substring("#ItemName".Length).Trim().TrimStart('=');
tmp.ItemName = str;
}
else if (str.StartsWith("#DBType"))
{
str = str.Substring("#DBType".Length).Trim().TrimStart('=');
tmp.DBType = str;
}
else if (str.StartsWith("#IP"))
{
str = str.Substring("#IP".Length).Trim().TrimStart('=');
tmp.IP = str;
}
else if (str.StartsWith("#DBName"))
{
str = str.Substring("#DBName".Length).Trim().TrimStart('=');
tmp.DBName = str;
}
else if (str.StartsWith("#UserID"))
{
str = str.Substring("#UserID".Length).Trim().TrimStart('=');
tmp.UserID = str;
}
else if (str.StartsWith("#PWD"))
{
str = str.Substring("#PWD".Length).Trim().TrimStart('=');
tmp.PWD = str;
}
if (string.IsNullOrEmpty(tmp.ItemID))
{
System.Threading.Thread.Sleep(10);
tmp.ItemID = DateTime.Now.ToString("yyyyMMddHHmmssfff");
}
}
}
if (DefaultItem == null && Items.Count > 0)
{
DefaultItem = Items[0];
}
}
public void WriteConf()
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "conn.conf");
StringBuilder sb = new StringBuilder();
if (DefaultItem != null)
{
sb.AppendLine("#Item_Start");
sb.AppendLine(" #ItemID=" + DefaultItem.ItemID);
sb.AppendLine(" #ItemName=" + DefaultItem.ItemName);
sb.AppendLine(" #DBType=" + DefaultItem.DBType);
sb.AppendLine(" #IP=" + DefaultItem.IP);
sb.AppendLine(" #DBName=" + DefaultItem.DBName);
sb.AppendLine(" #UserID=" + DefaultItem.UserID);
sb.AppendLine(" #PWD=" + DefaultItem.PWD);
sb.AppendLine("#Item_End");
}
for (int i = 0; i < Items.Count; i++)
{
if (Items[i].ItemName != DefaultItem.ItemName)
{
sb.AppendLine("#Item_Start");
sb.AppendLine(" #ItemID=" + Items[i].ItemID);
sb.AppendLine(" #ItemName=" + Items[i].ItemName);
sb.AppendLine(" #DBType=" + Items[i].DBType);
sb.AppendLine(" #IP=" + Items[i].IP);
sb.AppendLine(" #DBName=" + Items[i].DBName);
sb.AppendLine(" #UserID=" + Items[i].UserID);
sb.AppendLine(" #PWD=" + Items[i].PWD);
sb.AppendLine("#Item_End");
}
}
if (File.Exists(path))
{
File.Delete(path);
}
File.AppendAllText(path, sb.ToString());
}
public class ConntectItem
{
public string ItemID { set; get; }
public string ItemName { set; get; }
public string DBType { set; get; }
public string IP { set; get; }
public string DBName { set; get; }
public string UserID { set; get; }
public string PWD { set; get; }
}
}
}