-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryCommon.cs
More file actions
79 lines (67 loc) · 2.4 KB
/
LibraryCommon.cs
File metadata and controls
79 lines (67 loc) · 2.4 KB
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Dynamic;
using Newtonsoft.Json;
using System.Collections.Specialized;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
namespace DNA64.Library {
public static class Common {
public static dynamic json_decode(string data) {
return JValue.Parse(data);
}
public static string json_encode(dynamic data) {
return JsonConvert.SerializeObject (data, Formatting.Indented);
}
public static bool isset(dynamic settings, string key) {
if (settings is ExpandoObject) {
return ((IDictionary<string, object>)settings).ContainsKey(key);
} else if (settings is JObject) {
return settings.ContainsKey(key);
}
return settings.GetType().GetProperty(key) != null;
}
public static ExpandoObject DeepCopy (ExpandoObject original) {
var clone = new ExpandoObject ();
var _original = (IDictionary<string, object>)original;
var _clone = (IDictionary<string, object>)clone;
foreach (var kvp in _original)
_clone.Add (kvp.Key, kvp.Value is ExpandoObject ? DeepCopy ((ExpandoObject)kvp.Value) : kvp.Value);
return clone;
}
public static object CloneObject(object obj) {
dynamic temp = new ExpandoObject();
temp.Object = obj;
string json_data = json_encode(temp);
temp = json_decode(json_data);
return temp.Object;
}
public static void DeleteAll(string folder) {
System.IO.DirectoryInfo di = new DirectoryInfo (folder);
foreach (FileInfo file in di.GetFiles ()) {
file.Delete ();
}
foreach (DirectoryInfo dir in di.GetDirectories ()) {
dir.Delete (true);
}
}
public static void Empty (this System.IO.DirectoryInfo directory) {
foreach (System.IO.FileInfo file in directory.GetFiles ()) file.Delete ();
foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories ()) subDirectory.Delete (true);
}
public static string MD5(this string str) {
using (var cryptoMD5 = System.Security.Cryptography.MD5.Create()) {
var bytes = Encoding.UTF8.GetBytes(str);
var hash = cryptoMD5.ComputeHash(bytes);
var md5 = BitConverter.ToString(hash)
.Replace("-", String.Empty)
.ToUpper();
return md5;
}
}
}
}