-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectoryUtils.cs
71 lines (61 loc) · 2.36 KB
/
DirectoryUtils.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
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
namespace DirTemplateExtension
{
internal class DirectoryUtils
{
public static bool IsSpecialFolder(string path)
{
var directoryInfo = new DirectoryInfo(path);
return Enum.GetValues(typeof(Environment.SpecialFolder)).Cast<Environment.SpecialFolder>()
.Any(currentDir => directoryInfo.FullName == Environment.GetFolderPath(currentDir));
}
public static bool IsEqualOrChildOf(DirectoryInfo targetDir, DirectoryInfo parentDir)
{
var isChildOrEqual = parentDir.FullName.Equals(targetDir.FullName);
try
{
while (targetDir.Parent != null && !isChildOrEqual)
{
if (targetDir.Parent.FullName.Equals(parentDir.FullName))
{
isChildOrEqual = true;
break;
}
targetDir = targetDir.Parent;
}
}
catch
{
// ignored
}
return isChildOrEqual;
}
public static void CopyAll(DirectoryInfo source, DirectoryInfo target, BackgroundWorker backgroundWorker = null)
{
foreach (var file in source.GetFiles())
{
if (file.Name.Equals(Configuration.ProjectImageFile, StringComparison.OrdinalIgnoreCase))
{
continue;
}
Logger.Debug($"Copying file '{file.Name}' to '{target.FullName}'");
if (backgroundWorker != null && backgroundWorker.CancellationPending) break;
file.CopyTo(Path.Combine(target.FullName, file.Name), true);
}
foreach (var dir in source.GetDirectories())
{
Logger.Debug($"Creating directory '{dir.Name}' in '{target.FullName}'");
if (backgroundWorker != null && backgroundWorker.CancellationPending) break;
var nextTargetSubDir = target.CreateSubdirectory(dir.Name);
CopyAll(dir, nextTargetSubDir);
}
}
public static bool IsDirectoryEmpty(DirectoryInfo dirInfo)
{
return !Directory.EnumerateFileSystemEntries(dirInfo.FullName).Any();
}
}
}