-
Notifications
You must be signed in to change notification settings - Fork 1
/
XMLSaved.cs
314 lines (281 loc) · 12.5 KB
/
XMLSaved.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Security;
using System.Security.Cryptography;
using System.IO;
/// <summary>
/// Summary description for Class1
/// </summary>
namespace System.Xml
{
[Serializable]
public class XMLSaved<T>
{
/// <summary>
/// Ñîõðàíåíèå ñòðóêòóðû â ôàéë
/// </summary>
/// <param name="file">Ïîëíûé ïóòü ê ôàéëó</param>
/// <param name="obj">Ñòðóêòóðà</param>
public static void Save(string file, T obj)
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.IO.StreamWriter writer = System.IO.File.CreateText(file);
xs.Serialize(writer, obj);
writer.Flush();
writer.Close();
}
public static string Save(T obj)
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.IO.MemoryStream ms = new MemoryStream();
System.IO.StreamWriter writer = new StreamWriter(ms);
xs.Serialize(writer, obj);
writer.Flush();
ms.Position = 0;
byte[] bb = new byte[ms.Length];
ms.Read(bb, 0, bb.Length);
writer.Close();
return System.Text.Encoding.UTF8.GetString(bb); ;
}
/// <summary>
/// Ïîäêëþ÷åíèå ñòðóêòóðû èç ôàéëà
/// </summary>
/// <param name="file">Ïîëíûé ïóòü ê ôàéëó</param>
/// <returns>Ñòðóêòóðà</returns>
public static T Load(string file)
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.IO.StreamReader reader = System.IO.File.OpenText(file);
T c = (T)xs.Deserialize(reader);
reader.Close();
return c;
}
public static T LoadText(string text)
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
byte[] bb = System.Text.Encoding.UTF8.GetBytes(text);
ms.Write(bb, 0, bb.Length);
ms.Flush();
ms.Position = 0;
System.IO.StreamReader reader = new System.IO.StreamReader(ms);
T c = (T)xs.Deserialize(reader);
reader.Close();
return c;
}
/// <summary>
/// Ïîäêëþ÷åíèå ñòðóêòóðû èç ôàéëà
/// </summary>
/// <param name="file">Ïîëíûé ïóòü ê ôàéëó</param>
/// <returns>Ñòðóêòóðà</returns>
public static T LoadFile(string file) { return Load(file); }
/// <summary>
/// Ïîäêëþ÷åíèå ñòðóêòóðû èç URL
/// </summary>
/// <param name="url">Ññûëêà</param>
/// <returns>Ñòðóêòóðà</returns>
public static T LoadURL(string url)
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.Net.WebRequest wr = System.Net.HttpWebRequest.Create(url);
wr.Method = "GET";
wr.Timeout = 30000;
System.Net.WebResponse rp = wr.GetResponse();
System.IO.Stream ss = rp.GetResponseStream();
T c = (T)xs.Deserialize(ss);
ss.Close();
rp.Close();
return c;
}
/// <summary>
/// Ïîëó÷åíèå ïàïêè, èç êîòîðîé çàïóùåíî ïðèëîæåíèå
/// </summary>
/// <returns>Ïîëíûé ïóòü ê ïàïêè ñ \ íà êîíöå</returns>
public static string GetCurrentDir()
{
string fname = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString();
fname = fname.Replace("file:///", "");
fname = fname.Replace("/", @"\");
fname = fname.Substring(0, fname.LastIndexOf(@"\") + 1);
return fname;
}
/// <summary>
/// Ïîäêëþ÷åíèå îáúåêòà êëàññà ïî èíòåðôåéñó èç DLL
/// Êîíñòðóêòîð êëàññà äîëæåí áûòü áåç àðãóìåíòîâ
/// </summary>
/// <param name="filename">Ïîëíûé ïóòü ê ôàéëó</param>
/// <returns>Îáúåêò êëàññà</returns>
public static T LoadFromDLL(string filename)
{
System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(filename);
Type[] tps = asm.GetTypes();
Type asmType = null;
foreach (Type tp in tps) if (tp.GetInterface(typeof(T).ToString()) != null) asmType = tp;
System.Reflection.ConstructorInfo ci = asmType.GetConstructor(new Type[] { });
try
{
return (T)ci.Invoke(new object[] { });
}
catch (Exception ex)
{
string rr = GetReference(filename);
if (rr != "") rr = "\r\nAssembly references: " + rr;
throw new Exception(" Couldn't load assembly " + System.IO.Path.GetFileName(filename) + " - " + ex.Message + rr);
};
}
public static string GetReference(string filename)
{
try
{
System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(filename);
Type[] tps = asm.GetTypes();
Type asmType = null;
foreach (Type tp in tps)
if (tp.Name == "References") asmType = tp;
System.Reflection.MethodInfo mi = asmType.GetMethod("Reference");
return (string)mi.Invoke(null, null);
}
catch (Exception ex)
{
return "";
};
}
/// <summary>
/// Ïîäêëþ÷åíèå îáúåêòà êëàññà ïî èíòåðôåéñó èç DLL ïî URL
/// Êîíñòðóêòîð êëàññà äîëæåí áûòü áåç àðãóìåíòîâ
/// (Èñïîëüçóåòñÿ äëÿ èíòåðôåéñîâ)
/// </summary>
/// <param name="url">Ññûëêà</param>
/// <returns>Îáúåêò êëàññà</returns>
public static T LoadFromDLL_URL(string url)
{
System.Net.WebRequest wr = System.Net.HttpWebRequest.Create(url);
wr.Method = "GET";
wr.Timeout = 30000;
System.Net.WebResponse rp = wr.GetResponse();
System.IO.Stream ss = rp.GetResponseStream();
string dd = System.Environment.SpecialFolder.ApplicationData.ToString() + @"\#tmplda\";
if (!System.IO.Directory.Exists(dd)) System.IO.Directory.CreateDirectory(dd);
string f = DateTime.UtcNow.Ticks.ToString();
f = f.Substring(f.Length - 7);
string ff = dd + f + ".dll";
System.IO.FileStream fs = new FileStream(ff, FileMode.CreateNew);
int rb = -1;
while ((rb = ss.ReadByte()) >= 0) fs.WriteByte((byte)rb);
ss.Close();
fs.Close();
rp.Close();
System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(ff);
Type[] tps = asm.GetTypes();
Type asmType = null;
foreach (Type tp in tps) if (tp.GetInterface(typeof(T).ToString()) != null) asmType = tp;
System.Reflection.ConstructorInfo ci = asmType.GetConstructor(new Type[] { });
return (T)ci.Invoke(new object[] { });
}
/// <summary>
/// Ïîäêëþ÷åíèå îáúåêòà êëàññà ïî òèïó ÷åðåç Remoting
/// (Ex: tcp://dns_host_name:port/uri )
/// </summary>
/// <param name="url"></param>
/// <returns>Îáúåêò êëàññà</returns>
public static T LoadRemoteDLL_URL(string url)
{
return (T)System.Runtime.Remoting.RemotingServices.Connect(typeof(T), url);
}
/// <summary>
/// Îòêëþ÷åíèå îáúåêòà êëàññà îò Remoting
/// </summary>
/// <param name="obj"></param>
/// <returns>Ðåçóëüòàò</returns>
public static bool UnLoadRemoteDLL(object obj)
{
return System.Runtime.Remoting.RemotingServices.Disconnect((MarshalByRefObject)obj);
}
/// <summary>
/// Øèôðîâàíèå òåêñòà
/// </summary>
/// <param name="textIn">òåêñò</param>
/// <returns>HASH òåêñòà</returns>
public static string CodeInString(string textIn)
{
string txt = textIn.Trim();
while (txt.Length < 32) txt += " ";
byte[] code_key = new byte[] { 194, 61, 213, 244, 141, 188, 172, 76, 251, 99, 252, 139, 89, 2, 205, 240, 27, 216, 181, 245, 240, 171, 239, 113, 22, 234, 66, 87, 246, 100, 87, 214 };
byte[] code_IV = new byte[] { 149, 7, 196, 206, 172, 5, 33, 229, 55, 242, 113, 7, 75, 72, 170, 149 };
System.Security.Cryptography.RijndaelManaged rm = new RijndaelManaged();
rm.Padding = PaddingMode.None;
ICryptoTransform ict = rm.CreateEncryptor(code_key, code_IV);
System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, ict, CryptoStreamMode.Write);
System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt);
swEncrypt.Write(txt);
swEncrypt.Flush();
byte[] bb = msEncrypt.ToArray();
swEncrypt.Close();
csEncrypt.Close();
msEncrypt.Close();
string res = "";
for (int i = 0; i < bb.Length; i++) res += String.Format("{0:X2}", bb[i]);
return res;
}
/// <summary>
/// Äåøèôðîâêà òåêñòà
/// </summary>
/// <param name="textIn">HASH òåêñòà</param>
/// <returns>òåêñò</returns>
public static string CodeOutString(string textIn)
{
string txt = textIn;
byte[] bytes_in = new byte[(int)(txt.Length / 2)];
for (int i = 0; i < bytes_in.Length; i++)
{
string ct = txt.Substring(i * 2, 2);
bytes_in[i] = (byte)int.Parse(ct, System.Globalization.NumberStyles.HexNumber);
};
byte[] code_key = new byte[] { 194, 61, 213, 244, 141, 188, 172, 76, 251, 99, 252, 139, 89, 2, 205, 240, 27, 216, 181, 245, 240, 171, 239, 113, 22, 234, 66, 87, 246, 100, 87, 214 };
byte[] code_IV = new byte[] { 149, 7, 196, 206, 172, 5, 33, 229, 55, 242, 113, 7, 75, 72, 170, 149 };
System.Security.Cryptography.RijndaelManaged rm = new RijndaelManaged();
rm.Padding = PaddingMode.None;
ICryptoTransform ict = rm.CreateDecryptor(code_key, code_IV);
System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream(bytes_in);
CryptoStream csEncrypt = new CryptoStream(msEncrypt, ict, CryptoStreamMode.Read);
System.IO.StreamReader swEncrypt = new System.IO.StreamReader(csEncrypt);
string res = swEncrypt.ReadToEnd();
swEncrypt.Close();
csEncrypt.Close();
msEncrypt.Close();
return res.Trim();
}
/// <summary>
/// Äîáàâëÿåì îøèáêó â ñèñòåìíûé ëîã
/// </summary>
/// <param name="msg"></param>
public static void AddErr2SysLog(string msg)
{
try
{
string sSource = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
if (!System.Diagnostics.EventLog.SourceExists(sSource))
System.Diagnostics.EventLog.CreateEventSource(sSource, "Application");
System.Diagnostics.EventLog.WriteEntry(sSource, msg, System.Diagnostics.EventLogEntryType.Error);
}
catch { };
}
/// <summary>
/// Äîáàâëÿåì â ñèñòåìíûé ëîã
/// </summary>
/// <param name="msg"></param>
public static void Add2SysLog(string msg)
{
try
{
string sSource = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
if (!System.Diagnostics.EventLog.SourceExists(sSource))
System.Diagnostics.EventLog.CreateEventSource(sSource, "Application");
System.Diagnostics.EventLog.WriteEntry(sSource, msg, System.Diagnostics.EventLogEntryType.Information);
}
catch { };
}
}
}