This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectoryUtil.cs
61 lines (51 loc) · 2.15 KB
/
DirectoryUtil.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
using System;
using System.IO;
namespace JPPSVN {
public static class DirectoryUtil {
private static void CopyFiles(DirectoryInfo directory, string destDirName) {
foreach (FileInfo file in directory.EnumerateFiles()) {
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, true);
}
}
private static void CopySubDirectories(DirectoryInfo directory, string destDirName, bool copySubDirs, Predicate<string> topLevelFolderPredicate) {
foreach (DirectoryInfo subdir in directory.EnumerateDirectories()) {
if(!topLevelFolderPredicate(subdir.Name))
continue;
string temppath = Path.Combine(destDirName, subdir.Name);
Copy(subdir, temppath, copySubDirs, s => true);
}
}
private static void Copy(DirectoryInfo dir, string destDirName, bool copySubDirs, Predicate<string> topLevelFolderPredicate) {
// If the destination directory doesn't exist, create it.
DirectoryInfo destination = new DirectoryInfo(destDirName);
if (!destination.Exists)
destination.Create();
CopyFiles(dir, destDirName);
// If copying subdirectories, copy them and their contents to new location.
if(!copySubDirs) return;
CopySubDirectories(dir, destDirName, copySubDirs, topLevelFolderPredicate);
}
public static void CopyIgnoreNotExists(string sourceDirName, string destDirName, bool copySubDirs, Predicate<string> topLevelFolderPredicate) {
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if(dir.Exists)
Copy(dir, destDirName, copySubDirs, topLevelFolderPredicate);
}
public static void CopyIgnoreNotExists(string sourceDirName, string destDirName, bool copySubDirs) {
CopyIgnoreNotExists(sourceDirName, destDirName, copySubDirs, s => true);
}
public static void DeleteDirectory(string path) {
foreach(string directory in Directory.GetDirectories(path)) {
DeleteDirectory(directory);
}
try {
Directory.Delete(path, true);
} catch(IOException) {
Directory.Delete(path, true);
} catch(UnauthorizedAccessException) {
Directory.Delete(path, true);
}
}
}
}