-
Notifications
You must be signed in to change notification settings - Fork 6
/
DirectoryManager.cs
81 lines (72 loc) · 2.81 KB
/
DirectoryManager.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
using Microsoft.Extensions.Logging;
namespace ODataApiGen
{
public class DirectoryManager
{
private ILogger Logger {get; } = Program.LoggerFactory.CreateLogger<DirectoryManager>();
public string Output {get; private set;}
public DirectoryInfo DirectoryInfo {get; private set;}
public void PrepareOutput(bool purgeOutput)
{
Logger.LogDebug($"Preparing output path '{Output}'...");
if (Directory.Exists(Output) && purgeOutput)
{
Logger.LogInformation("Purging output directory...");
Directory.Delete(Output, true);
}
Logger.LogInformation("Folder doesn't exists, creating...");
Directory.CreateDirectory(Output);
}
public void PrepareFolders(IEnumerable<string> directories)
{
var dsList = directories.ToList();
dsList.Sort();
foreach (var ds in dsList)
{
if (!Directory.Exists( Path.Combine(DirectoryInfo.FullName, ds)))
{
Logger.LogDebug($"Creating subdirectory '{ds}'");
DirectoryInfo.CreateSubdirectory(ds);
}
}
}
public DirectoryManager(string outFolder)
{
Output = outFolder;
DirectoryInfo = new DirectoryInfo(outFolder);
}
public void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// 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(destDirName, file.Name);
file.CopyTo(temppath, true);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
}