-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExt.cs
More file actions
272 lines (244 loc) · 7.28 KB
/
Copy pathExt.cs
File metadata and controls
272 lines (244 loc) · 7.28 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
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace System.Windows.Media
{
public static class ColorExtension
{
public static Color FadeTo(this Color color, Color toColor, float percent)
{
return Color.FromArgb((byte)Lerp(color.A, toColor.A, percent), (byte)Lerp(color.R, toColor.R, percent), (byte)Lerp(color.G, toColor.G, percent), (byte)Lerp(color.B, toColor.B, percent));
}
private static float Lerp(float v0, float v1, float t)
{
return v0 + (v1 - v0) * t;
}
}
}
namespace System
{
public static class Int64Extension
{
public static string ToReadableByteSize(this long @long)
{
return @long.ToReadableByteSize("0.## ");
}
public static string ToReadableByteSize(this long @long, string format)
{
string str;
string sign = @long < 0 ? "-" : string.Empty;
double num;
if (@long >= 1152921504606846976L)
{
str = "EB";
num = @long >> 50;
}
else if (@long >= 1125899906842624L)
{
str = "PB";
num = @long >> 40;
}
else if (@long >= 1099511627776L)
{
str = "TB";
num = @long >> 30;
}
else if (@long >= 1073741824)
{
str = "GB";
num = @long >> 20;
}
else if (@long < 1048576)
{
if (@long < 1024)
{
return @long.ToString(string.Concat(sign, "0 B"));
}
str = "KB";
num = @long;
}
else
{
str = "MB";
num = @long >> 10;
}
num = num / 1024;
return string.Concat(sign, num.ToString(format), str);
}
}
public static class StringExtension
{
public static int IndexOfAny(this string @string, char[] anyOf, out char? foundChar)
{
return @string.IndexOfAny(anyOf, 0, out foundChar);
}
public static int IndexOfAny(this string @string, char[] anyOf, int startIndex, out char? foundChar)
{
return @string.IndexOfAny(anyOf, startIndex, @string.Length - startIndex, out foundChar);
}
public static int IndexOfAny(this string @string, char[] anyOf, int startIndex, int count, out char? foundChar)
{
foundChar = null;
int num = @string.IndexOfAny(anyOf, startIndex, count);
if (num > -1)
{
bool flag = false;
for (int i = 0; i < anyOf.Length && !flag; i++)
{
char? nullable = anyOf[i];
char? nullable1 = nullable;
foundChar = nullable;
char? nullable2 = nullable1;
int str = @string[num];
flag = nullable2.GetValueOrDefault() == (char)str;
}
}
return num;
}
public static int IndexOfAny(this string @string, string[] anyOf)
{
return @string.IndexOfAny(anyOf, 0);
}
public static int IndexOfAny(this string @string, string[] anyOf, out string foundString)
{
return @string.IndexOfAny(anyOf, 0, out foundString);
}
public static int IndexOfAny(this string @string, string[] anyOf, int startIndex)
{
return @string.IndexOfAny(anyOf, startIndex, @string.Length - startIndex);
}
public static int IndexOfAny(this string @string, string[] anyOf, int startIndex, out string foundString)
{
return @string.IndexOfAny(anyOf, startIndex, @string.Length - startIndex, out foundString);
}
public static int IndexOfAny(this string @string, string[] anyOf, int startIndex, int count)
{
return @string.IndexOfAny(anyOf, startIndex, count, out string _);
}
public static int IndexOfAny(this string @string, string[] anyOf, int startIndex, int count, out string foundString)
{
Dictionary<string, int> strs = new Dictionary<string, int>();
string[] strArrays = anyOf;
foreach (string str in strArrays)
{
int num = @string.IndexOf(str, startIndex, count, StringComparison.Ordinal);
if (num > -1)
{
strs[str] = num;
}
}
if (!strs.Any())
{
foundString = null;
return -1;
}
KeyValuePair<string, int> keyValuePair = strs.ElementAt(0);
foundString = keyValuePair.Key;
return keyValuePair.Value;
}
public static int LastIndexOfAny(this string @string, char[] anyOf, out char? foundChar)
{
return @string.LastIndexOfAny(anyOf, @string.Length - 1, out foundChar);
}
public static int LastIndexOfAny(this string @string, char[] anyOf, int startIndex, out char? foundChar)
{
return @string.LastIndexOfAny(anyOf, startIndex, startIndex + 1, out foundChar);
}
public static int LastIndexOfAny(this string @string, char[] anyOf, int startIndex, int count, out char? foundChar)
{
foundChar = null;
int num = @string.LastIndexOfAny(anyOf, startIndex, count);
if (num > -1)
{
bool flag = false;
for (int i = 0; i < anyOf.Length && !flag; i++)
{
char? nullable = anyOf[i];
char? nullable1 = nullable;
foundChar = nullable;
char? nullable2 = nullable1;
int str = @string[num];
flag = (nullable2.GetValueOrDefault() == (char)str);
}
}
return num;
}
public static int LastIndexOfAny(this string @string, string[] anyOf)
{
return @string.LastIndexOfAny(anyOf, @string.Length - 1);
}
public static int LastIndexOfAny(this string @string, string[] anyOf, out string foundString)
{
return @string.LastIndexOfAny(anyOf, @string.Length - 1, out foundString);
}
public static int LastIndexOfAny(this string @string, string[] anyOf, int startIndex)
{
return @string.LastIndexOfAny(anyOf, startIndex, out string _);
}
public static int LastIndexOfAny(this string @string, string[] anyOf, int startIndex, out string foundString)
{
return @string.LastIndexOfAny(anyOf, startIndex, startIndex + 1, out foundString);
}
public static int LastIndexOfAny(this string @string, string[] anyOf, int startIndex, int count)
{
return @string.LastIndexOfAny(anyOf, startIndex, count, out string _);
}
public static int LastIndexOfAny(this string @string, string[] anyOf, int startIndex, int count, out string foundString)
{
Dictionary<string, int> strs = new Dictionary<string, int>();
string[] strArrays = anyOf;
foreach (string str in strArrays)
{
int num = @string.LastIndexOf(str, startIndex, count, StringComparison.Ordinal);
if (num > -1)
{
strs[str] = num;
}
}
if (!strs.Any())
{
foundString = null;
return -1;
}
KeyValuePair<string, int> keyValuePair = strs.ElementAt(0);
foundString = keyValuePair.Key;
return keyValuePair.Value;
}
}
}
namespace System.Diagnostics
{
public static class ProcessExtension
{
private static readonly Dictionary<int, DateTime> CpuCheckTimes;
private static readonly Dictionary<int, TimeSpan> CpuTimes;
static ProcessExtension()
{
CpuTimes = new Dictionary<int, TimeSpan>();
CpuCheckTimes = new Dictionary<int, DateTime>();
}
public static double GetCpuUsage(this Process process)
{
process.Refresh();
if (!CpuTimes.ContainsKey(process.Id))
{
process.InitCpuUsage();
}
TimeSpan totalProcessorTime = process.TotalProcessorTime;
DateTime utcNow = DateTime.UtcNow;
TimeSpan item = totalProcessorTime - CpuTimes[process.Id];
double totalSeconds = item.TotalSeconds;
TimeSpan timeSpan = utcNow - CpuCheckTimes[process.Id];
double num = timeSpan.TotalSeconds;
double processorCount = totalSeconds / (Environment.ProcessorCount * num);
CpuTimes[process.Id] = totalProcessorTime;
CpuCheckTimes[process.Id] = utcNow;
return processorCount;
}
public static void InitCpuUsage(this Process process)
{
CpuTimes[process.Id] = process.TotalProcessorTime;
CpuCheckTimes[process.Id] = DateTime.UtcNow;
}
}
}