-
Notifications
You must be signed in to change notification settings - Fork 34
/
FileSystem.cs
513 lines (439 loc) · 16.5 KB
/
FileSystem.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
//-----------------------------------------------------------------------
// <copyright file="FileSystem.cs" company="Gavin Kendall">
// Copyright (c) 2008-2023 Gavin Kendall
// </copyright>
// <author>Gavin Kendall</author>
// <summary>All the methods used for basic IO operations such as creating files, deleting files, and checking if drives are ready.</summary>
//
// 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 3 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, see <https://www.gnu.org/licenses/>.
//-----------------------------------------------------------------------
using System;
using System.Drawing;
using System.IO;
namespace AutoScreenCapture
{
/// <summary>
/// Anything involving files and folders are defined in this class.
/// </summary>
public class FileSystem
{
/// <summary>
/// The path delimiter to use.
/// </summary>
public readonly string PathDelimiter = Path.DirectorySeparatorChar.ToString();
/// <summary>
/// The file manager to execute whenever the user chooses to open their screenshots folder or edits the selected screenshot.
/// </summary>
public readonly string FileManager = "explorer";
/// <summary>
/// The name of the log files that get messages written if logging is enabled. The Log class uses its own file extension so only the name is used here.
/// </summary>
public readonly string LogFile = "autoscreen-log";
/// <summary>
/// The error file containing possible exception error messages when the application encounters an error.
/// </summary>
public readonly string ErrorFile = "autoscreen-error.txt";
/// <summary>
/// The name of the Auto Screen Capture Configuration File.
/// </summary>
public string ConfigFile = AppDomain.CurrentDomain.BaseDirectory + "autoscreen.conf";
/// <summary>
/// This is to be backwards compatible with an old version of the application that used the "slides" folder.
/// </summary>
public readonly string SlidesFolder = AppDomain.CurrentDomain.BaseDirectory + "!autoscreen\\slides\\";
/// <summary>
/// The file to create when the application is starting up and needing to log its startup procedure.
/// </summary>
public readonly string StartupLogFile = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Auto Screen Capture\\autoscreen_startup_log.txt";
/// <summary>
/// The file to create when the user runs an instance of autoscreen while existing instances are already running.
/// </summary>
public readonly string StartupErrorFile = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Auto Screen Capture\\autoscreen_startup_error.txt";
/// <summary>
/// Screenshots folder.
/// </summary>
public string ScreenshotsFolder;
/// <summary>
/// Filename pattern.
/// </summary>
public string FilenamePattern;
/// <summary>
/// Errors folder.
/// </summary>
public string ErrorsFolder;
/// <summary>
/// Logs folder.
/// </summary>
public string LogsFolder;
/// <summary>
/// Command file.
/// </summary>
public string CommandFile;
/// <summary>
/// Application settings file.
/// </summary>
public string ApplicationSettingsFile;
/// <summary>
/// SMTP settings file.
/// </summary>
public string SmtpSettingsFile;
/// <summary>
/// SFTP settings file.
/// </summary>
public string SftpSettingsFile;
/// <summary>
/// User settings file.
/// </summary>
public string UserSettingsFile;
/// <summary>
/// Editors file.
/// </summary>
public string EditorsFile;
/// <summary>
/// Regions file.
/// </summary>
public string RegionsFile;
/// <summary>
/// Screens file.
/// </summary>
public string ScreensFile;
/// <summary>
/// Triggers file.
/// </summary>
public string TriggersFile;
/// <summary>
/// Screenshots file.
/// </summary>
public string ScreenshotsFile;
/// <summary>
/// Tags file.
/// </summary>
public string MacroTagsFile;
/// <summary>
/// Schedules file.
/// </summary>
public string SchedulesFile;
/// <summary>
/// A class for handling file system methods.
/// </summary>
public FileSystem()
{
}
/// <summary>
/// Just in case the user gives us an empty folder path or forgets
/// to include the trailing backslash for the screenshots folder.
/// </summary>
/// <param name="screenshotsFolderPath">The path of the screenshots folder to correct.</param>
/// <returns>A corrected version of the screenshots folder path.</returns>
public string CorrectScreenshotsFolderPath(string screenshotsFolderPath)
{
if (string.IsNullOrEmpty(screenshotsFolderPath) || screenshotsFolderPath.Length <= 0)
{
screenshotsFolderPath = ScreenshotsFolder;
}
if (!screenshotsFolderPath.EndsWith(PathDelimiter))
{
screenshotsFolderPath += PathDelimiter;
}
return screenshotsFolderPath;
}
/// <summary>
/// Deletes directories and files recursively in a specified directory and deletes the specified directory.
/// </summary>
/// <param name="dirName">The starting "parent" directory (which will also be deleted after everything else inside it is deleted).</param>
public void DeleteDirectory(string dirName)
{
if (DirectoryExists(dirName))
{
DirectoryInfo di = new DirectoryInfo(dirName);
foreach (FileInfo file in di.EnumerateFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.EnumerateDirectories())
{
dir.Delete(true);
}
Directory.Delete(dirName, true);
}
}
/// <summary>
/// Determines if the directory exists for a given path.
/// </summary>
/// <param name="path">The path to check if the directory exists.</param>
/// <returns>True if the directory exists. False if the directory does not exist.</returns>
public bool DirectoryExists(string path)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
return Directory.Exists(path);
}
/// <summary>
/// Creates a directory for a given path.
/// </summary>
/// <param name="path">The path of the directory to create.</param>
public void CreateDirectory(string path)
{
if (string.IsNullOrEmpty(path))
{
return;
}
Directory.CreateDirectory(path);
}
/// <summary>
/// Determines if the file exists for a given path.
/// </summary>
/// <param name="path">The path of the file to check.</param>
/// <returns>True if the file exists. False if the file does not exist.</returns>
public bool FileExists(string path)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
return File.Exists(path);
}
/// <summary>
/// Determines if the file in the given path has a file extension.
/// </summary>
/// <param name="path">The path of the file to check.</param>
/// <returns>True if the file has a file extension. False if the file does not have a file extension.</returns>
public bool HasExtension(string path)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
return Path.HasExtension(path);
}
/// <summary>
/// Creates a new file given the path of the file.
/// </summary>
/// <param name="path">The path of the file to create.</param>
public void CreateFile(string path)
{
if (string.IsNullOrEmpty(path))
{
return;
}
File.Create(path).Dispose();
}
/// <summary>
/// Gets the free disk space percentage of the drive for a given path.
/// </summary>
/// <param name="path">The path of the drive to check.</param>
/// <returns>The free disk space percentage.</returns>
public double FreeDiskSpacePercentage(string path)
{
if (string.IsNullOrEmpty(path))
{
return 0;
}
FileInfo fileInfo = new FileInfo(path);
DriveInfo driveInfo = new DriveInfo(fileInfo.Directory.Root.FullName);
return (driveInfo.AvailableFreeSpace / (float)driveInfo.TotalSize) * 100;
}
/// <summary>
/// Gets the free disk space of the drive for a given path.
/// </summary>
/// <param name="path">The path of the drive to check.</param>
/// <returns>The available free space (in bytes)</returns>
public long FreeDiskSpace(string path)
{
FileInfo fileInfo = new FileInfo(path);
DriveInfo driveInfo = new DriveInfo(fileInfo.Directory.Root.FullName);
return driveInfo.AvailableFreeSpace;
}
/// <summary>
/// Determines if the drive of the given path is ready.
/// </summary>
/// <param name="path">The path to check for the drive's status.</param>
/// <returns>True if the drive is ready. False if the drive is not ready.</returns>
public bool DriveReady(string path)
{
if (string.IsNullOrEmpty(path) || path.StartsWith(PathDelimiter))
{
return false;
}
FileInfo fileInfo = new FileInfo(path);
DriveInfo driveInfo = new DriveInfo(fileInfo.Directory.Root.FullName);
return driveInfo.IsReady;
}
/// <summary>
/// Gets the image from a given path.
/// </summary>
/// <param name="path">The path of the image file to get the image from.</param>
/// <returns>The image of an image file.</returns>
public Image GetImage(string path)
{
if (string.IsNullOrEmpty(path))
{
return null;
}
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
return Image.FromStream(stream);
}
}
/// <summary>
/// Gets the name of the directory from a given path.
/// </summary>
/// <param name="path">The path to get the directory name.</param>
/// <returns>The directory name of the given path.</returns>
public string GetDirectoryName(string path)
{
if (string.IsNullOrEmpty(path))
{
return string.Empty;
}
return Path.GetDirectoryName(path);
}
/// <summary>
/// Gets the filename of the given path.
/// </summary>
/// <param name="path">The path to get the filename from.</param>
/// <returns>The filename of the given path.</returns>
public string GetFileName(string path)
{
if (string.IsNullOrEmpty(path))
{
return string.Empty;
}
return Path.GetFileName(path);
}
/// <summary>
/// Gets the fully qualified name of the given path (stripping any whitespace in between sub-folders).
/// </summary>
/// <param name="path">The path to get the full path from.</param>
/// <returns>The full path (stripped from any whitespace in between sub-folders).</returns>
public string GetFullPath(string path)
{
if (string.IsNullOrEmpty(path))
{
return string.Empty;
}
return Path.GetFullPath(path);
}
/// <summary>
/// Writes a line to a file.
/// </summary>
/// <param name="path">The path to the file to be written.</param>
/// <param name="line">The line of text to write to the file.</param>
public void WriteToFile(string path, string line)
{
if (string.IsNullOrEmpty(path))
{
return;
}
File.WriteAllText(path, line);
}
/// <summary>
/// Writes multiple lines to a file.
/// </summary>
/// <param name="path">The path to the file to be written.</param>
/// <param name="linesToWrite">The array of lines to write to the file.</param>
public void WriteToFile(string path, string[] linesToWrite)
{
if (string.IsNullOrEmpty(path))
{
return;
}
File.WriteAllLines(path, linesToWrite);
}
/// <summary>
/// Appends a line to an existing file.
/// </summary>
/// <param name="path">The path to the file to be written.</param>
/// <param name="line">The line to append to the file.</param>
public void AppendToFile(string path, string line)
{
if (string.IsNullOrEmpty(path))
{
return;
}
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(line);
sw.Flush();
sw.Close();
}
}
/// <summary>
/// Reads from a file.
/// </summary>
/// <param name="path">The path to the file to read from.</param>
/// <returns>An array of lines representing the lines of text in the file.</returns>
public string[] ReadFromFile(string path)
{
if (string.IsNullOrEmpty(path))
{
return null;
}
return File.ReadAllLines(path);
}
/// <summary>
/// Gets the directory separator character.
/// </summary>
/// <returns>The directory separator character.</returns>
public char DirectorySeparatorChar()
{
return Path.DirectorySeparatorChar;
}
/// <summary>
/// Deletes a file.
/// </summary>
/// <param name="path">The path of the file to delete.</param>
public bool DeleteFile(string path)
{
try
{
if (string.IsNullOrEmpty(path) || !File.Exists(path))
{
return false;
}
File.Delete(path);
return true;
}
catch (IOException)
{
return false;
}
}
/// <summary>
/// Moves a file.
/// </summary>
/// <param name="source">The source filepath.</param>
/// <param name="destination">The destination filepath.</param>
public void MoveFile(string source, string destination)
{
File.Move(source, destination);
}
/// <summary>
/// Gets the length of a file's contents.
/// </summary>
/// <param name="path">The path to the file to check.</param>
/// <returns>The number of characters in the file.</returns>
public long FileContentLength(string path)
{
if (string.IsNullOrEmpty(path))
{
return -1;
}
FileInfo fileInfo = new FileInfo(path);
return fileInfo.Length;
}
}
}