forked from Wox-launcher/Wox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesFolders.cs
125 lines (109 loc) · 4.17 KB
/
FilesFolders.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
using NLog;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using Wox.Infrastructure.Logger;
namespace Wox.Infrastructure
{
public static class FilesFolders
{
private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger();
public static void Copy(this string sourcePath, string targetPath)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourcePath);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourcePath);
}
try
{
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(targetPath, file.Name);
file.CopyTo(temppath, false);
}
// Recursively copy subdirectories by calling itself on each subdirectory until there are no more to copy
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(targetPath, subdir.Name);
Copy(subdir.FullName, temppath);
}
}
catch (System.Exception e)
{
string message = $"Copying path {targetPath} has failed, it will now be deleted for consistency";
Logger.WoxError(message, e);
MessageBox.Show(message);
RemoveFolderIfExists(targetPath);
}
}
public static bool VerifyBothFolderFilesEqual(this string fromPath, string toPath)
{
try
{
var fromDir = new DirectoryInfo(fromPath);
var toDir = new DirectoryInfo(toPath);
if (fromDir.GetFiles("*", SearchOption.AllDirectories).Length != toDir.GetFiles("*", SearchOption.AllDirectories).Length)
return false;
if (fromDir.GetDirectories("*", SearchOption.AllDirectories).Length != toDir.GetDirectories("*", SearchOption.AllDirectories).Length)
return false;
return true;
}
catch (System.Exception e)
{
string message = $"Unable to verify folders and files between {fromPath} and {toPath}";
Logger.WoxError(message, e);
MessageBox.Show(message);
return false;
}
}
public static void RemoveFolderIfExists(this string path)
{
try
{
if (Directory.Exists(path))
Directory.Delete(path, true);
}
catch (System.Exception e)
{
string message = $"Not able to delete folder { (object)path}, please go to the location and manually delete it";
Logger.WoxError(message, e);
MessageBox.Show(message);
}
}
public static bool LocationExists(this string path)
{
return Directory.Exists(path);
}
public static bool FileExits(this string filePath)
{
return File.Exists(filePath);
}
public static void OpenLocationInExporer(string location)
{
try
{
if (LocationExists(location))
Process.Start(location);
}
catch (System.Exception e)
{
string message = $"Unable to open location { (object)location}, please check if it exists";
Logger.WoxError(message, e);
MessageBox.Show(message);
}
}
}
}