This repository has been archived by the owner on Jul 18, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Helpers.cs
124 lines (108 loc) · 4.89 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
#region License Information (GPL v3)
/*
Source code provocatively stolen from ShareX: https://github.com/ShareX/ShareX.
(Seriously, awesome work over there, I took some parts of the Code to make
ImgurSniper.)
Their License:
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 System;
using System.IO;
using System.Windows.Media;
namespace ImgurSniper.Libraries.Helper {
public static class Helpers {
public static void CreateDirectoryFromFilePath(string path) {
if (!string.IsNullOrEmpty(path)) {
CreateDirectoryFromDirectoryPath(Path.GetDirectoryName(path));
}
}
public static void CreateDirectoryFromDirectoryPath(string path) {
if (!string.IsNullOrEmpty(path) && !Directory.Exists(path)) {
try {
Directory.CreateDirectory(path);
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
}
public static string GetAbsolutePath(string path) {
path = ExpandFolderVariables(path);
if (!Path.IsPathRooted(path)) // Is relative path?
{
path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
}
return Path.GetFullPath(path);
}
public static string ExpandFolderVariables(string path) {
if (!string.IsNullOrEmpty(path)) {
try {
// ReSharper disable once AccessToModifiedClosure
GetEnums<Environment.SpecialFolder>().ForEach(x => path = path.Replace($"%{x}%", Environment.GetFolderPath(x), StringComparison.InvariantCultureIgnoreCase));
path = Environment.ExpandEnvironmentVariables(path);
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
return path;
}
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) {
Console.WriteLine(e.Message);
}
}
return path;
}
public static T[] GetEnums<T>() {
return (T[])Enum.GetValues(typeof(T));
}
//Play the Blop Success Sound
public static void PlayBlop() {
try {
MediaPlayer player = new MediaPlayer { Volume = 30 };
player.MediaEnded += delegate {
player.Close();
};
string path = Path.Combine(ConfigHelper.InstallDir, "Resources\\Blop.wav");
player.Open(new Uri(path));
player.Play();
} catch {
// ignored
}
}
public static void WriteError(Exception ex) {
try {
string errorFile = Path.Combine(ConfigHelper.DocumentsDirectory, "error.txt");
string nl = Environment.NewLine;
string errorDetails = $"!ImgurSniper Error @{DateTime.Now}" + nl +
$" Error Message: {ex.Message}" + nl + nl +
$" Error Stacktrace: {ex.StackTrace}";
if (File.Exists(errorFile)) {
File.AppendAllText(errorFile,
nl + nl + "---------------------------------------------------------" + errorDetails);
} else {
File.WriteAllText(errorFile,
$"Details for an Exception in ImgurSniper. " +
"You can tell me about this error on http://www.github.com/mrousavy/ImgurSniper/issues so I can fix it as soon as possible!"
+ nl + errorDetails);
}
} catch { }
}
}
}