-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileUtilities.cs
133 lines (121 loc) · 6.01 KB
/
FileUtilities.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
using System.Diagnostics;
namespace BetterSnippingTool.Utilities
{
public static class FileUtilities
{
public static string ExecutableDir { get; private set; }
public static string BaseDir { get; private set; }
public static string BaseTempDir { get; private set; }
public static string GifTempDir { get; private set; }
public static string GifTempScreenshotsDir { get; private set; }
public static string FFmpegDir { get; private set; }
public static string ResourcesDir { get; private set; }
public static string ProfilesDir { get; private set; }
public static Dictionary<string, string> ButtonImagePaths { get; private set; }
static FileUtilities()
{
//Initialize the base directory and depdency directories
ExecutableDir = AppDomain.CurrentDomain.BaseDirectory;
BaseDir = GetBaseDir();
FFmpegDir = Path.Combine(BaseDir, "ffmpeg", "ffmpeg.exe");
ResourcesDir = Path.Combine(BaseDir, "Resources");
//Temp Directories
BaseTempDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "BetterSnippingTool_Temp");
GifTempDir = Path.Combine(BaseTempDir, "GIF");
GifTempScreenshotsDir = Path.Combine(GifTempDir, "GIF_Temp_Screenshots");
ProfilesDir = Path.Combine(BaseTempDir, "Profiles");
//Create necessary temp folders if not already created
CreateTempFolders();
//Initialize the button image paths
ButtonImagePaths = InitializeButtonImagePaths();
}
//Method to determine base directory depending on the environment
private static string GetBaseDir()
{
string executableDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//Check if running in a development environment
if (IsDevelopmentEnvironment(executableDir))
{
//Running in Visual Studio (Debug or Release mode)
return Path.GetFullPath(Path.Combine(executableDir, @"..\..\..\"));
}
else
{
//Running in production (published app)
return Path.GetFullPath(executableDir);
}
}
// Method to initialize button image paths
private static Dictionary<string, string> InitializeButtonImagePaths()
{
return new Dictionary<string, string>
{
{ "BS_Logo", Path.Combine(ResourcesDir, "BS_Logo.ico") },
{ "green_crosshair", Path.Combine(ResourcesDir, "green_crosshair.png") },
{ "New_Snip_Button", Path.Combine(ResourcesDir, "Button_Images", "New_Snip_Button.png") },
{ "New_GIF_Button", Path.Combine(ResourcesDir, "Button_Images", "New_GIF_Button.png") },
{ "Save_Button", Path.Combine(ResourcesDir, "Button_Images", "Save_Button.png") },
{ "Trim_Button", Path.Combine(ResourcesDir, "Button_Images", "Trim_Button.png") },
{ "Paint_Button_Off", Path.Combine(ResourcesDir, "Button_Images", "Paint_Button_Off.png") },
{ "Paint_Button_On", Path.Combine(ResourcesDir, "Button_Images", "Paint_Button_On.png") },
{ "Trim_Button_Off", Path.Combine(ResourcesDir, "Button_Images", "Trim_Button_Off.png") },
{ "Trim_Button_On", Path.Combine(ResourcesDir, "Button_Images", "Trim_Button_On.png") },
{ "Play_Button", Path.Combine(ResourcesDir, "Button_Images", "Play_Button.png") },
{ "Pause_Button", Path.Combine(ResourcesDir, "Button_Images", "Pause_Button.png") },
{ "Stop_Button", Path.Combine(ResourcesDir, "Button_Images", "Stop_Button.png") },
{ "New_GIF_Button_REDO", Path.Combine(ResourcesDir, "Button_Images", "New_GIF_Button_REDO.png") },
{ "New_Snip_Button_REDO", Path.Combine(ResourcesDir, "Button_Images", "New_Snip_Button_REDO.png") },
{ "Settings_Button", Path.Combine(ResourcesDir, "Button_Images", "Settings_Button.png") },
{ "Exit_Button", Path.Combine(ResourcesDir, "Button_Images", "Exit_Button.png") }
};
}
//Creates temp directories if they don't exist
private static void CreateTempFolders()
{
try
{
//Create all required directories if they don't exist
Directory.CreateDirectory(BaseTempDir);
Directory.CreateDirectory(GifTempDir);
Directory.CreateDirectory(Path.Combine(GifTempDir, "GIF_Temp_Screenshots"));
Directory.CreateDirectory(ProfilesDir);
}
catch (Exception ex)
{
Console.WriteLine($"Error creating temp directories: {ex.Message}");
}
}
//Clears the temp directory of all temp files
public static void ClearTemp()
{
try
{
if (Directory.Exists(GifTempScreenshotsDir))
{
Array.ForEach(Directory.GetFiles(GifTempScreenshotsDir), File.Delete);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error clearing temp directory: {ex.Message}");
}
}
//Deletes all files in the specified GIF folder
public static void ClearGifFolder(string gifFilePath)
{
try
{
string gifFolderPath = Path.GetDirectoryName(gifFilePath);
Array.ForEach(Directory.GetFiles(gifFolderPath), File.Delete);
}
catch (Exception ex)
{
Console.WriteLine($"Error clearing GIF folder: {ex.Message}");
}
}
private static bool IsDevelopmentEnvironment(string executableDir)
{
return executableDir.Contains(@"\bin\Debug\") || executableDir.Contains(@"\bin\Release\");
}
}
}