-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Helpers.cs
1216 lines (1030 loc) · 39.6 KB
/
Helpers.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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2017 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using Microsoft.Win32;
using Newtonsoft.Json.Linq;
using ShareX.HelpersLib.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Media;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Windows.Forms;
namespace ShareX.HelpersLib
{
public static class Helpers
{
public const string Numbers = "0123456789"; // 48 ... 57
public const string AlphabetCapital = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 65 ... 90
public const string Alphabet = "abcdefghijklmnopqrstuvwxyz"; // 97 ... 122
public const string Alphanumeric = Numbers + AlphabetCapital + Alphabet;
public const string AlphanumericInverse = Numbers + Alphabet + AlphabetCapital;
public const string Hexadecimal = Numbers + "ABCDEF";
public const string URLCharacters = Alphanumeric + "-._~"; // 45 46 95 126
public const string URLPathCharacters = URLCharacters + "/"; // 47
public const string ValidURLCharacters = URLPathCharacters + ":?#[]@!$&'()*+,;= ";
public static readonly string[] ImageFileExtensions = new string[] { "jpg", "jpeg", "png", "gif", "bmp", "ico", "tif", "tiff" };
public static readonly string[] TextFileExtensions = new string[] { "txt", "log", "nfo", "c", "cpp", "cc", "cxx", "h", "hpp", "hxx", "cs", "vb", "html", "htm", "xhtml", "xht", "xml", "css", "js", "php", "bat", "java", "lua", "py", "pl", "cfg", "ini", "dart" };
public static readonly string[] VideoFileExtensions = new string[] { "mp4", "webm", "mkv", "avi", "vob", "ogv", "ogg", "mov", "qt", "wmv", "m4p", "m4v", "mpg", "mp2", "mpeg", "mpe", "mpv", "m2v", "m4v", "flv", "f4v" };
public static readonly Version OSVersion = Environment.OSVersion.Version;
/// <summary>Get file name extension without dot.</summary>
public static string GetFilenameExtension(string filePath)
{
if (!string.IsNullOrEmpty(filePath))
{
int pos = filePath.LastIndexOf('.');
if (pos >= 0)
{
return filePath.Substring(pos + 1);
}
}
return null;
}
public static string GetFilenameSafe(string filePath)
{
if (!string.IsNullOrEmpty(filePath))
{
int pos = filePath.LastIndexOf('\\');
if (pos < 0)
{
pos = filePath.LastIndexOf('/');
}
if (pos >= 0)
{
return filePath.Substring(pos + 1);
}
}
return filePath;
}
public static string ChangeFilenameExtension(string filePath, string extension)
{
if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(extension))
{
int pos = filePath.LastIndexOf('.');
if (pos >= 0)
{
filePath = filePath.Remove(pos);
extension = extension.Trim();
pos = extension.LastIndexOf('.');
if (pos >= 0)
{
extension = extension.Substring(pos + 1);
}
return filePath + "." + extension;
}
}
return filePath;
}
public static string RenameFile(string filePath, string newFileName)
{
try
{
if (File.Exists(filePath))
{
string directory = Path.GetDirectoryName(filePath);
string newPath = Path.Combine(directory, newFileName);
File.Move(filePath, newPath);
return newPath;
}
}
catch (Exception e)
{
MessageBox.Show("Rename error:\r\n" + e.ToString(), "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return filePath;
}
public static string AppendExtension(string filePath, string extension)
{
return filePath.TrimEnd('.') + '.' + extension.TrimStart('.');
}
public static bool CheckExtension(string filePath, IEnumerable<string> extensions)
{
string ext = GetFilenameExtension(filePath);
if (!string.IsNullOrEmpty(ext))
{
return extensions.Any(x => ext.Equals(x, StringComparison.InvariantCultureIgnoreCase));
}
return false;
}
public static bool IsImageFile(string filePath)
{
return CheckExtension(filePath, ImageFileExtensions);
}
public static bool IsTextFile(string filePath)
{
return CheckExtension(filePath, TextFileExtensions);
}
public static bool IsVideoFile(string filePath)
{
return CheckExtension(filePath, VideoFileExtensions);
}
public static EDataType FindDataType(string filePath)
{
if (IsImageFile(filePath))
{
return EDataType.Image;
}
if (IsTextFile(filePath))
{
return EDataType.Text;
}
return EDataType.File;
}
public static string AddZeroes(string input, int digits = 2)
{
return input.PadLeft(digits, '0');
}
public static string AddZeroes(int number, int digits = 2)
{
return AddZeroes(number.ToString(), digits);
}
public static string HourTo12(int hour)
{
if (hour == 0)
{
return 12.ToString();
}
if (hour > 12)
{
return AddZeroes(hour - 12);
}
return AddZeroes(hour);
}
public static char GetRandomChar(string chars)
{
return chars[MathHelpers.Random(chars.Length - 1)];
}
public static string GetRandomString(string chars, int length)
{
StringBuilder sb = new StringBuilder();
while (length-- > 0)
{
sb.Append(GetRandomChar(chars));
}
return sb.ToString();
}
public static string GetRandomNumber(int length)
{
return GetRandomString(Numbers, length);
}
public static string GetRandomAlphanumeric(int length)
{
return GetRandomString(Alphanumeric, length);
}
public static string GetRandomKey(int length = 5, int count = 3, char separator = '-')
{
return Enumerable.Range(1, (length + 1) * count - 1).Aggregate("", (x, index) => x += index % (length + 1) == 0 ? separator : GetRandomChar(Alphanumeric));
}
public static string GetAllCharacters()
{
return Encoding.UTF8.GetString(Enumerable.Range(1, 255).Select(i => (byte)i).ToArray());
}
public static string GetValidFileName(string fileName, string separator = "")
{
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
if (string.IsNullOrEmpty(separator))
{
return new string(fileName.Where(c => !invalidFileNameChars.Contains(c)).ToArray());
}
else
{
invalidFileNameChars.ForEach(x => fileName = fileName.Replace(x.ToString(), separator));
return fileName.Trim().Replace(separator + separator, separator);
}
}
public static string GetValidFolderPath(string folderPath)
{
char[] invalidPathChars = Path.GetInvalidPathChars();
return new string(folderPath.Where(c => !invalidPathChars.Contains(c)).ToArray());
}
public static string GetValidFilePath(string filePath)
{
string folderPath = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);
return GetValidFolderPath(folderPath) + Path.DirectorySeparatorChar + GetValidFileName(fileName);
}
public static string GetValidURL(string url, bool replaceSpace = false)
{
if (replaceSpace) url = url.Replace(' ', '_');
return HttpUtility.UrlPathEncode(url);
}
public static string GetXMLValue(string input, string tag)
{
return Regex.Match(input, string.Format("(?<={0}>).+?(?=</{0})", tag)).Value;
}
public static string GetMimeType(string fileName)
{
if (!string.IsNullOrEmpty(fileName))
{
string ext = Path.GetExtension(fileName).ToLower();
if (!string.IsNullOrEmpty(ext))
{
string mimeType = MimeTypes.GetMimeType(ext);
if (!string.IsNullOrEmpty(mimeType))
{
return mimeType;
}
using (RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(ext))
{
if (regKey != null && regKey.GetValue("Content Type") != null)
{
mimeType = regKey.GetValue("Content Type").ToString();
if (!string.IsNullOrEmpty(mimeType))
{
return mimeType;
}
}
}
}
}
return MimeTypes.DefaultMimeType;
}
public static T[] GetEnums<T>()
{
return (T[])Enum.GetValues(typeof(T));
}
public static string[] GetEnumDescriptions<T>()
{
return Enum.GetValues(typeof(T)).OfType<Enum>().Select(x => x.GetDescription()).ToArray();
}
/*public static string[] GetLocalizedEnumDescriptions<T>()
{
Assembly assembly = typeof(T).Assembly;
string resourcePath = assembly.GetName().Name + ".Properties.Resources";
ResourceManager resourceManager = new ResourceManager(resourcePath, assembly);
return GetLocalizedEnumDescriptions<T>(resourceManager);
}*/
public static string[] GetLocalizedEnumDescriptions<T>()
{
return GetLocalizedEnumDescriptions<T>(Resources.ResourceManager);
}
public static string[] GetLocalizedEnumDescriptions<T>(ResourceManager resourceManager)
{
return Enum.GetValues(typeof(T)).OfType<Enum>().Select(x => x.GetLocalizedDescription(resourceManager)).ToArray();
}
public static int GetEnumLength<T>()
{
return Enum.GetValues(typeof(T)).Length;
}
public static T GetEnumFromIndex<T>(int i)
{
return GetEnums<T>()[i];
}
public static string[] GetEnumNamesProper<T>()
{
string[] names = Enum.GetNames(typeof(T));
string[] newNames = new string[names.Length];
for (int i = 0; i < names.Length; i++)
{
newNames[i] = GetProperName(names[i]);
}
return newNames;
}
// returns a list of public static fields of the class type (similar to enum values)
public static T[] GetValueFields<T>()
{
List<T> res = new List<T>();
foreach (FieldInfo fi in typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public))
{
if (fi.FieldType != typeof(T)) continue;
res.Add((T)fi.GetValue(null));
}
return res.ToArray();
}
// Example: "TopLeft" becomes "Top left"
public static string GetProperName(string name)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < name.Length; i++)
{
char c = name[i];
if (i > 0 && char.IsUpper(c))
{
sb.Append(' ');
sb.Append(char.ToLowerInvariant(c));
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
public static bool OpenFile(string filePath)
{
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
try
{
Process.Start(filePath);
return true;
}
catch (Exception e)
{
DebugHelper.WriteException(e, $"OpenFile({filePath}) failed.");
}
}
else
{
MessageBox.Show(Resources.Helpers_OpenFile_File_not_exist_ + Environment.NewLine + filePath, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return false;
}
public static bool OpenFolder(string folderPath)
{
if (!string.IsNullOrEmpty(folderPath) && Directory.Exists(folderPath))
{
if (!folderPath.EndsWith(@"\"))
{
folderPath += @"\";
}
try
{
Process.Start(folderPath);
return true;
}
catch (Exception e)
{
DebugHelper.WriteException(e, $"OpenFolder({folderPath}) failed.");
}
}
else
{
MessageBox.Show(Resources.Helpers_OpenFolder_Folder_not_exist_ + Environment.NewLine + folderPath, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return false;
}
public static bool OpenFolderWithFile(string filePath)
{
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
try
{
NativeMethods.OpenFolderAndSelectFile(filePath);
return true;
}
catch (Exception e)
{
DebugHelper.WriteException(e, $"OpenFolderWithFile({filePath}) failed.");
}
}
else
{
MessageBox.Show(Resources.Helpers_OpenFile_File_not_exist_ + Environment.NewLine + filePath, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return false;
}
/// <summary>
/// If version1 newer than version2 = 1
/// If version1 equal to version2 = 0
/// If version1 older than version2 = -1
/// </summary>
public static int CompareVersion(string version1, string version2)
{
return NormalizeVersion(version1).CompareTo(NormalizeVersion(version2));
}
/// <summary>
/// If version1 newer than version2 = 1
/// If version1 equal to version2 = 0
/// If version1 older than version2 = -1
/// </summary>
public static int CompareVersion(Version version1, Version version2)
{
return version1.Normalize().CompareTo(version2.Normalize());
}
/// <summary>
/// If version newer than ApplicationVersion = 1
/// If version equal to ApplicationVersion = 0
/// If version older than ApplicationVersion = -1
/// </summary>
public static int CompareApplicationVersion(string version)
{
return CompareVersion(version, Application.ProductVersion);
}
private static Version NormalizeVersion(string version)
{
return Version.Parse(version).Normalize();
}
public static bool IsWindowsXP()
{
return OSVersion.Major == 5 && OSVersion.Minor == 1;
}
public static bool IsWindowsXPOrGreater()
{
return (OSVersion.Major == 5 && OSVersion.Minor >= 1) || OSVersion.Major > 5;
}
public static bool IsWindowsVista()
{
return OSVersion.Major == 6;
}
public static bool IsWindowsVistaOrGreater()
{
return OSVersion.Major >= 6;
}
public static bool IsWindows7()
{
return OSVersion.Major == 6 && OSVersion.Minor == 1;
}
public static bool IsWindows7OrGreater()
{
return (OSVersion.Major == 6 && OSVersion.Minor >= 1) || OSVersion.Major > 6;
}
public static bool IsWindows8()
{
return OSVersion.Major == 6 && OSVersion.Minor == 2;
}
public static bool IsWindows8OrGreater()
{
return (OSVersion.Major == 6 && OSVersion.Minor >= 2) || OSVersion.Major > 6;
}
public static bool IsWindows10OrGreater()
{
return OSVersion.Major >= 10;
}
public static bool IsDefaultInstallDir()
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
return Application.ExecutablePath.StartsWith(path);
}
public static bool IsValidIPAddress(string ip)
{
if (string.IsNullOrEmpty(ip)) return false;
string pattern = @"(?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Fourth>2[0-4]\d|25[0-5]|[01]?\d\d?)";
return Regex.IsMatch(ip.Trim(), pattern);
}
public static string GetUniqueFilePath(string filepath)
{
if (File.Exists(filepath))
{
string folder = Path.GetDirectoryName(filepath);
string filename = Path.GetFileNameWithoutExtension(filepath);
string extension = Path.GetExtension(filepath);
int number = 1;
Match regex = Regex.Match(filepath, @"(.+) \((\d+)\)\.\w+");
if (regex.Success)
{
filename = regex.Groups[1].Value;
number = int.Parse(regex.Groups[2].Value);
}
do
{
number++;
filepath = Path.Combine(folder, string.Format("{0} ({1}){2}", filename, number, extension));
}
while (File.Exists(filepath));
}
return filepath;
}
public static string ProperTimeSpan(TimeSpan ts)
{
string time = string.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds);
int hours = (int)ts.TotalHours;
if (hours > 0) time = hours + ":" + time;
return time;
}
public static object Clone(object obj)
{
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter binaryFormatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
binaryFormatter.Serialize(ms, obj);
ms.Seek(0, SeekOrigin.Begin);
return binaryFormatter.Deserialize(ms);
}
}
public static void PlaySoundAsync(Stream stream)
{
if (stream != null)
{
TaskEx.Run(() =>
{
using (stream)
using (SoundPlayer soundPlayer = new SoundPlayer(stream))
{
soundPlayer.PlaySync();
}
});
}
}
public static void PlaySoundAsync(string filepath)
{
if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath))
{
TaskEx.Run(() =>
{
using (SoundPlayer soundPlayer = new SoundPlayer(filepath))
{
soundPlayer.PlaySync();
}
});
}
}
public static bool BrowseFile(TextBox tb, string initialDirectory = "", bool detectSpecialFolders = false)
{
return BrowseFile("ShareX - " + Resources.Helpers_BrowseFile_Choose_file, tb, initialDirectory, detectSpecialFolders);
}
public static bool BrowseFile(string title, TextBox tb, string initialDirectory = "", bool detectSpecialFolders = false)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Title = title;
try
{
string path = tb.Text;
if (!string.IsNullOrEmpty(path))
{
path = Path.GetDirectoryName(path);
if (Directory.Exists(path))
{
ofd.InitialDirectory = path;
}
}
}
finally
{
if (string.IsNullOrEmpty(ofd.InitialDirectory) && !string.IsNullOrEmpty(initialDirectory))
{
ofd.InitialDirectory = initialDirectory;
}
}
if (ofd.ShowDialog() == DialogResult.OK)
{
tb.Text = detectSpecialFolders ? GetVariableFolderPath(ofd.FileName) : ofd.FileName;
return true;
}
}
return false;
}
public static bool BrowseFolder(TextBox tb, string initialDirectory = "", bool detectSpecialFolders = false)
{
return BrowseFolder("ShareX - " + Resources.Helpers_BrowseFolder_Choose_folder, tb, initialDirectory, detectSpecialFolders);
}
public static bool BrowseFolder(string title, TextBox tb, string initialDirectory = "", bool detectSpecialFolders = false)
{
using (FolderSelectDialog fsd = new FolderSelectDialog())
{
fsd.Title = title;
string path = tb.Text;
if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
{
fsd.InitialDirectory = path;
}
else if (!string.IsNullOrEmpty(initialDirectory))
{
fsd.InitialDirectory = initialDirectory;
}
if (fsd.ShowDialog())
{
tb.Text = detectSpecialFolders ? GetVariableFolderPath(fsd.FileName) : fsd.FileName;
return true;
}
}
return false;
}
public static string GetVariableFolderPath(string path)
{
if (!string.IsNullOrEmpty(path))
{
try
{
GetEnums<Environment.SpecialFolder>().ForEach(x => path = path.Replace(Environment.GetFolderPath(x), $"%{x}%", StringComparison.InvariantCultureIgnoreCase));
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
}
return path;
}
public static string ExpandFolderVariables(string path)
{
if (!string.IsNullOrEmpty(path))
{
try
{
GetEnums<Environment.SpecialFolder>().ForEach(x => path = path.Replace($"%{x}%", Environment.GetFolderPath(x), StringComparison.InvariantCultureIgnoreCase));
path = Environment.ExpandEnvironmentVariables(path);
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
}
return path;
}
public static bool WaitWhile(Func<bool> check, int interval, int timeout = -1)
{
Stopwatch timer = Stopwatch.StartNew();
while (check())
{
if (timeout >= 0 && timer.ElapsedMilliseconds >= timeout)
{
return false;
}
Thread.Sleep(interval);
}
return true;
}
public static void WaitWhileAsync(Func<bool> check, int interval, int timeout, Action onSuccess, int waitStart = 0)
{
bool result = false;
TaskEx.Run(() =>
{
if (waitStart > 0)
{
Thread.Sleep(waitStart);
}
result = WaitWhile(check, interval, timeout);
},
() =>
{
if (result) onSuccess();
}, false);
}
public static bool IsFileLocked(string path)
{
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
}
}
catch (IOException)
{
return true;
}
return false;
}
public static void CreateDirectoryFromDirectoryPath(string path)
{
if (!string.IsNullOrEmpty(path) && !Directory.Exists(path))
{
try
{
Directory.CreateDirectory(path);
}
catch (Exception e)
{
DebugHelper.WriteException(e);
MessageBox.Show(Resources.Helpers_CreateDirectoryIfNotExist_Create_failed_ + "\r\n\r\n" + e, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public static void CreateDirectoryFromFilePath(string path)
{
if (!string.IsNullOrEmpty(path))
{
CreateDirectoryFromDirectoryPath(Path.GetDirectoryName(path));
}
}
public static void BackupFileMonthly(string filepath, string destinationFolder)
{
if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath))
{
string filename = Path.GetFileNameWithoutExtension(filepath);
string extension = Path.GetExtension(filepath);
string newFilename = string.Format("{0}-{1:yyyy-MM}{2}", filename, DateTime.Now, extension);
string newFilepath = Path.Combine(destinationFolder, newFilename);
if (!File.Exists(newFilepath))
{
CreateDirectoryFromFilePath(newFilepath);
File.Copy(filepath, newFilepath, false);
}
}
}
public static void BackupFileWeekly(string filepath, string destinationFolder)
{
if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath))
{
string filename = Path.GetFileNameWithoutExtension(filepath);
DateTime dateTime = DateTime.Now;
string extension = Path.GetExtension(filepath);
string newFilename = string.Format("{0}-{1:yyyy-MM}-W{2:00}{3}", filename, dateTime, dateTime.WeekOfYear(), extension);
string newFilepath = Path.Combine(destinationFolder, newFilename);
if (!File.Exists(newFilepath))
{
CreateDirectoryFromFilePath(newFilepath);
File.Copy(filepath, newFilepath, false);
}
}
}
public static string GetUniqueID()
{
return Guid.NewGuid().ToString("N");
}
public static Point GetPosition(ContentAlignment placement, Point offset, Size backgroundSize, Size objectSize)
{
int midX = backgroundSize.Width / 2 - objectSize.Width / 2;
int midY = backgroundSize.Height / 2 - objectSize.Height / 2;
int right = backgroundSize.Width - objectSize.Width;
int bottom = backgroundSize.Height - objectSize.Height;
switch (placement)
{
default:
case ContentAlignment.TopLeft:
return new Point(offset.X, offset.Y);
case ContentAlignment.TopCenter:
return new Point(midX, offset.Y);
case ContentAlignment.TopRight:
return new Point(right - offset.X, offset.Y);
case ContentAlignment.MiddleLeft:
return new Point(offset.X, midY);
case ContentAlignment.MiddleCenter:
return new Point(midX, midY);
case ContentAlignment.MiddleRight:
return new Point(right - offset.X, midY);
case ContentAlignment.BottomLeft:
return new Point(offset.X, bottom - offset.Y);
case ContentAlignment.BottomCenter:
return new Point(midX, bottom - offset.Y);
case ContentAlignment.BottomRight:
return new Point(right - offset.X, bottom - offset.Y);
}
}
public static Size MeasureText(string text, Font font)
{
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
return g.MeasureString(text, font).ToSize();
}
}
public static Size MeasureText(string text, Font font, int width)
{
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
return g.MeasureString(text, font, width).ToSize();
}
}
public static string SendPing(string host)
{
return SendPing(host, 1);
}
public static string SendPing(string host, int count)
{
string[] status = new string[count];
using (Ping ping = new Ping())
{
PingReply reply;
//byte[] buffer = Encoding.ASCII.GetBytes(new string('a', 32));
for (int i = 0; i < count; i++)
{
reply = ping.Send(host, 3000);
if (reply.Status == IPStatus.Success)
{
status[i] = reply.RoundtripTime.ToString() + " ms";
}
else
{
status[i] = "Timeout";
}
Thread.Sleep(100);
}
}
return string.Join(", ", status);
}
public static string DownloadString(string url)
{
if (!string.IsNullOrEmpty(url))
{
try
{
using (WebClient wc = new WebClient())
{
wc.Encoding = Encoding.UTF8;
wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
return wc.DownloadString(url);
}
}
catch (Exception e)
{
DebugHelper.WriteException(e);
MessageBox.Show(Resources.Helpers_DownloadString_Download_failed_ + "\r\n" + e, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return null;
}
public static void SetDefaultUICulture(CultureInfo culture)
{
Type type = typeof(CultureInfo);
try
{
// .NET 4.0
type.InvokeMember("s_userDefaultUICulture", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static, null, culture, new object[] { culture });
}
catch
{
try