-
Notifications
You must be signed in to change notification settings - Fork 375
/
DirectoryInfoExtensions.cs
76 lines (63 loc) · 2.16 KB
/
DirectoryInfoExtensions.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
namespace Microsoft.DotNet.Interactive.CSharpProject;
public static class DirectoryInfoExtensions
{
public static void CopyTo(
this DirectoryInfo source,
DirectoryInfo destination,
Func<FileSystemInfo,bool> skipWhen = null)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (!source.Exists)
{
throw new DirectoryNotFoundException(source.FullName);
}
if (!destination.Exists)
{
destination.Create();
}
foreach (var file in source.GetFiles())
{
if (skipWhen?.Invoke(file) == true)
{
continue;
}
file.CopyTo(
Path.Combine(
destination.FullName, file.Name), false);
}
foreach (var subdirectory in source.GetDirectories())
{
if (skipWhen?.Invoke(subdirectory) == true)
{
continue;
}
subdirectory.CopyTo(
new DirectoryInfo(
Path.Combine(
destination.FullName, subdirectory.Name)));
}
}
public static DirectoryInfo Subdirectory(this DirectoryInfo directoryInfo, string path)
{
return new DirectoryInfo(Path.Combine(directoryInfo.FullName, path));
}
public static FileInfo File(this DirectoryInfo directoryInfo, string name)
{
return new FileInfo(Path.Combine(directoryInfo.FullName, name));
}
public static DirectoryInfo NormalizeEnding(this DirectoryInfo directoryInfo)
{
if (!directoryInfo.FullName.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
return new DirectoryInfo(Path.Combine(directoryInfo.FullName, Path.DirectorySeparatorChar.ToString()));
}
return directoryInfo;
}
}