-
Notifications
You must be signed in to change notification settings - Fork 1
/
DirUtil.cs
152 lines (139 loc) · 5.25 KB
/
DirUtil.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
using System;
using System.IO;
namespace DotStd
{
/// <summary>
/// Helper util for directories.
/// </summary>
public static class DirUtil
{
/// <summary>
/// .NET oddly lacks a deep/recursive directory copy function.
/// </summary>
/// <param name="sourceDirName"></param>
/// <param name="destDirName"></param>
/// <param name="skipIfExists"></param>
public static void DirCopy(string sourceDirName, string destDirName, bool skipIfExists)
{
var dir = new DirectoryInfo(sourceDirName);
// If the source directory does not exist, throw an exception.
if (!dir.Exists)
{
// throw new DirectoryNotFoundException( "Source directory does not exist or could not be found: " + sourceDirName);
return;
}
// If the destination directory does not exist, create it.
if (Directory.Exists(destDirName))
{
if (skipIfExists)
return;
}
else
{
Directory.CreateDirectory(destDirName);
}
// Get the file contents of the directory to copy.
FileInfo[] files = dir.GetFiles();
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (FileInfo file in files)
{
// Create the path to the new copy of the file.
string temppath = Path.Combine(destDirName, file.Name);
// Copy the file.
file.CopyTo(temppath, false);
}
foreach (DirectoryInfo subdir in dirs)
{
// Create the subdirectory.
string tempPath = Path.Combine(destDirName, subdir.Name);
// Copy the sub-directories.
DirCopy(subdir.FullName, tempPath, false);
}
}
/// <summary>
/// Will create any missing parent directories as well.
/// like VB FileIO.FileSystem.DirectoryExists, FileIO.FileSystem.CreateDirectory
/// ignore if the dir already exists.
/// Will throw on failure.
/// </summary>
/// <param name="sDir"></param>
/// <returns>true = the dir needed to be created. it didn't exist.</returns>
public static bool DirCreate(string? sDir)
{
if (String.IsNullOrEmpty(sDir)) // this dir.
return false;
if (System.IO.Directory.Exists(sDir)) // is this needed?
return false;
System.IO.Directory.CreateDirectory(sDir);
return true;
}
public static bool DirCreateForFile(string filePath)
{
// I'm about to open a file for writing. make sure the directory exists.
return DirUtil.DirCreate(Path.GetDirectoryName(filePath));
}
/// <summary>
/// Delete all files in a directory. Possibly recursive.
/// remove ReadOnly bits.
/// </summary>
/// <param name="dirPath"></param>
/// <param name="bRecursive"></param>
/// <returns></returns>
public static int DirEmpty(string dirPath, bool bRecursive = false)
{
if (!System.IO.Directory.Exists(dirPath)) // avoid DirectoryNotFoundException
return -1;
int iCount = 0;
string[] files = Directory.GetFiles(dirPath);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal); // remove ReadOnly bit
File.Delete(file);
iCount++;
}
if (bRecursive)
{
string[] dirs = Directory.GetDirectories(dirPath);
foreach (string sDir2 in dirs)
{
iCount += DirDelete(sDir2);
}
}
return iCount;
}
public static int DirDelete(string sDir)
{
// Empty a dir then delete the dir itself. recursive.
int iCount = DirEmpty(sDir, true);
if (iCount < 0) // Empty it myself. i can change attributes.
return -1;
System.IO.Directory.Delete(sDir, false);
return iCount + 1;
}
public static int DirEmptyOld(string sDir, int nOlderThanHours = 24, string sPattern = "*.*")
{
// Delete just old stuff in this sDir.
if (!System.IO.Directory.Exists(sDir)) // avoid DirectoryNotFoundException
return -1; // nothing to delete
int iCount = 0;
try
{
DateTime tExpired = DateTime.UtcNow.Subtract(TimeSpan.FromHours(nOlderThanHours));
var dir = new DirectoryInfo(sDir);
foreach (System.IO.FileInfo file in dir.GetFiles(sPattern))
{
if (file.CreationTimeUtc < tExpired)
{
file.Delete();
iCount++;
}
}
}
catch (Exception)
{
// OK ignore errors, as this is not critical functionality
}
return iCount;
}
}
}