-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.cs
628 lines (576 loc) · 18.3 KB
/
Common.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Reflection;
namespace PaJaMa.Common
{
public partial class Common
{
public static string Md5Hash(string input)
{
// First we need to convert the string into bytes, which
// means using a text encoder.
Encoder enc = System.Text.Encoding.Unicode.GetEncoder();
// Create a buffer large enough to hold the string
byte[] unicodeText = new byte[input.Length * 2];
enc.GetBytes(input.ToCharArray(), 0, input.Length, unicodeText, 0, true);
// Now that we have a byte array we can ask the CSP to hash it
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(unicodeText);
// Build the final string by converting each byte
// into hex and appending it to a StringBuilder
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("X2"));
}
// And return it
return sb.ToString();
}
public static Guid Md5HashGuid(string input)
{
return new Guid(Md5Hash(input));
}
#region GetHTML
public static string GetHTML(string url, Dictionary<string, string> postParameters)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
Stream requestStream;
if (postParameters != null && postParameters.Count > 0)
{
string data = "";
foreach (string key in postParameters.Keys)
{
data += (data.Length > 0 ? "&" : "") + key + "=" + postParameters[key];
}
byte[] buffer = Encoding.ASCII.GetBytes(data);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
requestStream = response.GetResponseStream();
StreamReader sr = new StreamReader(requestStream);
return sr.ReadToEnd();
}
public static string GetHTML(string url)
{
return GetHTML(url, null);
}
#endregion
#region StringBytes
public static string GetStringFromStream(Stream stream)
{
//return GetStringFromStream(stream, (int)stream.Length);
//string rtv = string.Empty;
//StreamReader reader = new StreamReader(stream);
//return reader.ReadToEnd();
MemoryStream ms = new MemoryStream();
byte[] chunk = new byte[2048];
int bytesRead;
while ((bytesRead = stream.Read(chunk, 0, chunk.Length)) > 0)
{
ms.Write(chunk, 0, bytesRead);
//if (bytesRead < 2048) break;
}
return GetStringFromBytes(ms.GetBuffer());
}
public static string GetStringFromStream(Stream stream, int length)
{
byte[] bytes = new byte[length];
int count = stream.Read(bytes, 0, (int)bytes.Length);
String data = GetStringFromBytes(bytes);
char[] unused = { (char)data[count] };
return data.TrimEnd(unused);
}
public static string GetStringFromBytes(byte[] bytes)
{
return Encoding.UTF8.GetString(bytes);
}
public static byte[] GetBytesFromString(string input)
{
return Encoding.UTF8.GetBytes(input);
}
public static MemoryStream GetMemoryStreamFromString(string input)
{
return new MemoryStream(GetBytesFromString(input));
}
public static String UTF8ByteArrayToString(Byte[] bytes)
{
UTF8Encoding encoding = new UTF8Encoding();
String constructedString = encoding.GetString(bytes);
return (constructedString);
}
public static Byte[] StringToUTF8ByteArray(String xml)
{
UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(xml);
return byteArray;
}
public static string SpaceCameledCase(string input)
{
return System.Text.RegularExpressions.Regex.Replace(input, "([A-Z])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim();
}
public static byte[] StreamToBytes(Stream input, long length)
{
byte[] buffer = new byte[length];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
#endregion
#region SurroundingString
public static string SurroundingString(string full, string middle, int returnLength)
{
return SurroundingString(full, full.IndexOf(middle), middle.Length);
}
public static string SurroundingString(string full, int index, int length)
{
return SurroundingString(full, index, length, 50);
}
public static string SurroundingString(string full, string middle, int returnLength, int startIndex)
{
return SurroundingString(full, full.IndexOf(middle, startIndex), middle.Length);
}
public static string SurroundingString(string full, string middle)
{
return SurroundingString(full, middle, 50);
}
public static string SurroundingString(string full, int index, int length, int returnLength)
{
int tempLength = returnLength;
int start = Math.Max(index - (Math.Max(returnLength - length, 0) / 2), 0);
if (start + tempLength > full.Length)
{
start = full.Length - tempLength;
if (start < 0)
{
start = 0;
tempLength = full.Length;
}
}
return full.Substring(start, tempLength);
}
#endregion
#region CamelCaseToSpaced
public static string EnumCamelCaseToSpaced<T>(object enumValue)
{
return Enum.GetName(typeof(T), enumValue).CamelCaseToSpaced();
}
#endregion
#region MinMax
public static T Max<T>(params T[] values) where T : IComparable
{
if (values.Length < 1) return default(T);
T rtv = values[0];
foreach (T item in values)
{
if (item.CompareTo(rtv) > 0)
rtv = item;
}
return rtv;
}
public static T Min<T>(params T[] values) where T : IComparable
{
if (values.Length < 1) return default(T);
T rtv = values[0];
foreach (T item in values)
{
if (item.CompareTo(rtv) < 0)
rtv = item;
}
return rtv;
}
#endregion
#region Shutdown
[DllImport("user32.dll")]
private static extern void LockWorkStation();
public enum ShutdownType
{
LogOff = 0,
Shutdown = 1,
Reboot = 2,
ForcedLogOff = 4,
ForcedShutdown = 5,
ForcedReboot = 6,
PowerOff = 8,
ForcedPowerOff = 12,
Lock = 13,
Standby = 14,
Hibernate = 15
}
//public static void Shutdown(ShutdownType type)
//{
// switch (type)
// {
// case ShutdownType.Lock:
// LockWorkStation();
// break;
// case ShutdownType.Standby:
// Application.SetSuspendState(PowerState.Suspend, true, true);
// break;
// case ShutdownType.Hibernate:
// Application.SetSuspendState(PowerState.Hibernate, true, true);
// break;
// default:
// ManagementBaseObject mbo = null;
// ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
// mcWin32.Get();
// mcWin32.Scope.Options.EnablePrivileges = true;
// ManagementBaseObject mboParams =
// mcWin32.GetMethodParameters("Win32Shutdown");
// mboParams["Flags"] = ((int)type).ToString();
// mboParams["Reserved"] = "0";
// foreach (ManagementObject manObj in mcWin32.GetInstances())
// {
// mbo = manObj.InvokeMethod("Win32Shutdown",
// mboParams, null);
// }
// break;
// }
//}
#endregion
public static bool IsEmpty(object input)
{
if (input == null)
return true;
if (input == DBNull.Value)
return true;
if (string.IsNullOrEmpty(input.ToString()))
return true;
if (input is Guid && input.Equals(Guid.Empty))
return true;
return false;
}
#region ChangeType
public static T ChangeType<T>(object value)
{
TypeConverter tc = TypeDescriptor.GetConverter(typeof(T));
if (typeof(T).Equals(typeof(decimal)) || typeof(T).Equals(typeof(decimal?)))
return (T)tc.ConvertFrom(Convert.ToDecimal(value));
if (typeof(T).Equals(typeof(int)) || typeof(T).Equals(typeof(int?)))
return (T)tc.ConvertFrom(Convert.ToInt32(value));
return (T)tc.ConvertFrom(value);
}
#endregion
public static void RunInThread(Action action)
{
new System.Threading.Thread(new System.Threading.ThreadStart(action)).Start();
}
#region GetFilesRecursively
public static string[] GetFilesRecursively(string directoryName) { return GetFilesRecursively(directoryName, true); }
public static string[] GetFilesRecursively(string directoryName, bool includeDirectories)
{
List<string> rtv = new List<string>();
DirectoryInfo dinf = new DirectoryInfo(directoryName);
if (dinf.Exists)
{
foreach (FileInfo finf in dinf.GetFiles())
rtv.Add(finf.FullName);
foreach (DirectoryInfo dinf2 in dinf.GetDirectories())
{
if (includeDirectories)
rtv.Add(dinf2.FullName);
rtv.AddRange(GetFilesRecursively(dinf2.FullName, includeDirectories));
}
}
return rtv.ToArray();
}
public static List<FileInfo> GetFilesRecursively(DirectoryInfo dinf)
{
List<FileInfo> rtv = new List<FileInfo>();
foreach (FileInfo finf in dinf.GetFiles())
rtv.Add(finf);
foreach (DirectoryInfo dinf2 in dinf.GetDirectories())
{
rtv.AddRange(GetFilesRecursively(dinf2));
}
return rtv;
}
#endregion
#region Recycle
public static void Recycle(string fileName) { Recycle(fileName, true, true); }
public static void Recycle(string fileName, bool prompt) { Recycle(fileName, prompt, true); }
public static void Recycle(string fileName, bool prompt, bool showErrorUI)
{
Win32Api.InteropSHFileOperation fo = new Win32Api.InteropSHFileOperation();
fo.wFunc = Win32Api.InteropSHFileOperation.FO_Func.FO_DELETE;
fo.fFlags.FOF_ALLOWUNDO = true;
fo.fFlags.FOF_NOCONFIRMATION = !prompt;
fo.fFlags.FOF_NOERRORUI = !showErrorUI;
fo.pFrom = fileName;
int exec = fo.Execute();
if (exec == 0 || showErrorUI)
return;
switch (exec)
{
case 5:
throw new Exception("Could not delete '" + fileName + "', access denied.");
case 32:
throw new Exception("Could not delete '" + fileName + "', file is in use.");
default:
throw new Exception(exec.ToString());
}
}
#endregion
#region ExecuteFile
public static string ExecuteFile(string fileName) { return ExecuteFile(fileName, string.Empty, true); }
public static string ExecuteFile(string fileName, string arguments) { return ExecuteFile(fileName, arguments, true); }
public static string ExecuteFile(string fileName, string arguments, bool waitForExit)
{
if (string.IsNullOrEmpty(fileName) || !File.Exists(fileName)) return string.Empty;
FileInfo fInf = new FileInfo(fileName);
ProcessStartInfo psi = new ProcessStartInfo(fInf.FullName, arguments);
psi.UseShellExecute = false;
psi.ErrorDialog = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
Process p = Process.Start(psi);
StreamReader sr = p.StandardError;
string errOutput = sr.ReadToEnd();
sr.Close();
sr = p.StandardOutput;
string output = sr.ReadToEnd();
sr.Close();
if (waitForExit)
p.WaitForExit();
if (!string.IsNullOrEmpty(errOutput)) throw new Exception(errOutput);
return output;
}
#endregion
#region Reflection
public static void ReflectionSetValue(object field, string propertyName, object value)
{
System.Reflection.FieldInfo fi = field.GetType().GetField(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
fi.SetValue(field, value);
}
public static T CreateType<T>(string assemblyName, string typeName) { return CreateType<T>(assemblyName, Assembly.GetExecutingAssembly().Location, typeName, null); }
public static T CreateType<T>(string assemblyName, string typeName, object[] args) { return CreateType<T>(assemblyName, Assembly.GetExecutingAssembly().Location, typeName, args); }
public static T CreateType<T>(string assemblyName, string assemblyFileName, string typeName) { return CreateType<T>(assemblyName, assemblyFileName, typeName, null); }
public static T CreateType<T>(string assemblyName, string assemblyFileName, string typeName, object[] args)
{
try
{
Assembly asm = null;
T instance = default(T);
try
{
asm = Assembly.Load(assemblyName);
}
catch
{
asm = Assembly.LoadFile(assemblyFileName);
}
var type = (from t in asm.GetTypes()
where t.FullName == typeName
select t).FirstOrDefault();
if (type == null)
throw new Exception("Type could not be found in assembly.");
instance = (T)Activator.CreateInstance(type, args);
return instance;
}
catch (Exception ex)
{
throw new Exception(assemblyFileName + " is invalid or could not be found or " + typeName + " does not exist. Check inner exception for more details!", ex);
}
}
#endregion
#region GetType
public static Type GetType(string typeName) { return GetType(typeName, false, false); }
public static Type GetType(string typeName, bool throwOnError) { return GetType(typeName, throwOnError, false); }
public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
{
Type tp = Type.GetType(typeName, false, ignoreCase);
if (tp != null) return tp;
List<Type> types = new List<Type>();
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
tp = asm.GetType(typeName, false, ignoreCase);
if (tp == null)
{
types = new List<Type>();
types.AddRange(asm.GetTypes());
if (ignoreCase)
tp = types.Find(t => t.Name.ToLower() == typeName.ToLower() || t.FullName.ToLower() == typeName.ToLower());
else
tp = types.Find(t => t.Name == typeName || t.FullName == typeName);
if (tp != null)
break;
}
}
if (tp == null && throwOnError)
tp = GetType(typeName, true);
return tp;
}
public static string ParseStringFromWebPage(string url, string regexPattern) { return ParseStringFromWebPage(url, regexPattern, RegexOptions.None); }
public static string ParseStringFromWebPage(string url, string regexPattern, RegexOptions opts)
{
string match = string.Empty;
try
{
string html = new WebClient().DownloadString(new Uri(url));
Match m = Regex.Match(html, regexPattern, opts);
if (m.Success)
{
match = m.Groups[1].Value;
}
return match;
}
catch
{
return string.Empty;
}
}
#endregion
[DebuggerNonUserCode()]
public static bool ParseGUID(string input, out Guid outGuid)
{
outGuid = Guid.Empty;
if (!Regex.Match(input, @"^\{?[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}\}?$").Success)
return false;
try
{
outGuid = new Guid(input);
return true;
}
catch
{
return false;
}
}
public static bool IsValidGUID(string input)
{
Guid outGuid = Guid.Empty;
return ParseGUID(input, out outGuid);
}
public static TValue GetDictionaryValue<TKey, TValue>(Dictionary<TKey, TValue> dictionary, TKey key)
{
if (!dictionary.ContainsKey(key))
return default(TValue);
return dictionary[key];
}
public static void SetDictionaryValue<TKey, TValue>(Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (!dictionary.ContainsKey(key))
dictionary.Add(key, value);
else
dictionary[key] = value;
}
public static string Substring(string str, int startIndex, int length)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
if (str.Length <= startIndex)
return string.Empty;
if (str.Length <= length + startIndex)
return str.Substring(startIndex, str.Length - startIndex);
return str.Substring(startIndex, length);
}
public static string GetContentType(string fileName)
{
string contentType = "application/octetstream";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (registryKey != null && registryKey.GetValue("Content Type") != null)
contentType = registryKey.GetValue("Content Type").ToString();
return contentType;
}
public static string NullString(string str, string defaultValue)
{
return string.IsNullOrEmpty(str) ? defaultValue : str;
}
public static DateTime MinDate(DateTime dt1, DateTime dt2)
{
return dt1 < dt2 ? dt1 : dt2;
}
public static DateTime MaxDate(DateTime dt1, DateTime dt2)
{
return dt1 > dt2 ? dt1 : dt2;
}
public static string StripHTML(string input)
{
if (string.IsNullOrEmpty(input))
return string.Empty;
return Regex.Replace(input, @"<[^>]*>", String.Empty);
}
public static string PathSafeName(string input)
{
if (string.IsNullOrEmpty(input))
return input;
foreach (char c in Path.GetInvalidPathChars())
input = input.Replace(c.ToString(), "");
return input;
}
public static string FileSafeName(string input)
{
foreach (char c in Path.GetInvalidFileNameChars())
input = input.Replace(c.ToString(), "");
return input;
}
public static bool TryParseFraction(string input, out float fraction)
{
double tempDbl = -1;
fraction = 0;
if (!TryParseFraction(input, out tempDbl))
return false;
fraction = (float)tempDbl;
return true;
}
public static bool TryParseFraction(string input, out double fraction)
{
if (input.Contains("¼"))
input = input.Replace("¼", " 1/4");
if (input.Contains("½"))
input = input.Replace("½", " 1/2");
if (input.Contains("¾"))
input = input.Replace("¾", " 3/4");
if (input.Contains("⅓"))
input = input.Replace("⅓", " 1/3");
double? tempFraction = null;
fraction = 0;
string[] parts = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
foreach (string part in parts)
{
double tmpDbl = -1;
if (double.TryParse(part, out tmpDbl))
{
if (tempFraction == null) tempFraction = 0;
tempFraction += tmpDbl;
continue;
}
if (part.Contains("/"))
{
string[] parts2 = part.Split('/');
if (parts2.Length != 2) return false;
double numerator = -1;
double denominator = -1;
if (!double.TryParse(parts2[0], out numerator) || !double.TryParse(parts2[1], out denominator))
return false;
if (tempFraction == null) tempFraction = 0;
tempFraction += numerator / denominator;
}
}
fraction = tempFraction.GetValueOrDefault();
return tempFraction != null;
}
}
}