Skip to content

Commit 48f8936

Browse files
authored
feat(DirectoryInfoExtensions): add recursive parameter (#6358)
* refactor: 增加文件夹拷贝扩展方法 * test: 更新单元测试 * test: 更新单元测试
1 parent 6aa2bd6 commit 48f8936

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/BootstrapBlazor/Extensions/DirectoryInfoExtensions.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,44 @@ namespace BootstrapBlazor.Components;
1111
public static class DirectoryInfoExtensions
1212
{
1313
/// <summary>
14-
/// 文件夹拷贝方法
14+
/// Copies the contents of the current directory to a specified destination directory.
1515
/// </summary>
16-
/// <param name="sourceDirInfo"></param>
17-
/// <param name="destDirName"></param>
18-
public static void Copy(this DirectoryInfo sourceDirInfo, string destDirName)
16+
/// <remarks>This method creates the destination directory if it does not already exist. Files in the
17+
/// source directory are copied to the destination directory, and if <paramref name="recursive"/> is <see
18+
/// langword="true"/>, all subdirectories and their contents are also copied recursively.</remarks>
19+
/// <param name="dir">The source directory to copy from.</param>
20+
/// <param name="destinationDir">The path of the destination directory where the contents will be copied.</param>
21+
/// <param name="recursive"><see langword="true"/> to copy all subdirectories and their contents recursively; otherwise, <see
22+
/// langword="false"/>.</param>
23+
/// <exception cref="DirectoryNotFoundException">Thrown if the source directory specified by <paramref name="dir"/> does not exist.</exception>
24+
public static void Copy(this DirectoryInfo dir, string destinationDir, bool recursive = true)
1925
{
20-
if (!Directory.Exists(destDirName))
26+
// Check if the source directory exists
27+
if (!dir.Exists)
2128
{
22-
Directory.CreateDirectory(destDirName);
29+
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
2330
}
2431

25-
// CopyFile
26-
foreach (var info in sourceDirInfo.EnumerateFileSystemInfos())
32+
// Create the destination directory
33+
Directory.CreateDirectory(destinationDir);
34+
35+
// Get the files in the source directory and copy to the destination directory
36+
foreach (FileInfo file in dir.GetFiles())
2737
{
28-
if (info is FileInfo fi)
29-
{
30-
var targetFileName = Path.Combine(destDirName, info.Name);
31-
fi.CopyTo(targetFileName, true);
32-
}
33-
else if (info is DirectoryInfo di)
38+
string targetFilePath = Path.Combine(destinationDir, file.Name);
39+
file.CopyTo(targetFilePath);
40+
}
41+
42+
// If recursive and copying subdirectories, recursively call this method
43+
if (recursive)
44+
{
45+
// Cache directories before we start copying
46+
DirectoryInfo[] dirs = dir.GetDirectories();
47+
48+
foreach (DirectoryInfo subDir in dirs)
3449
{
35-
var targetFolderName = Path.Combine(destDirName, di.Name);
36-
Copy(di, targetFolderName);
50+
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
51+
Copy(subDir, newDestinationDir, true);
3752
}
3853
}
3954
}

test/UnitTest/Extensions/DirectoryInfoExtensionsTest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ public void Copy_Ok()
3232
}
3333

3434
var sourceDirInfo = new DirectoryInfo(sourceDir);
35-
sourceDirInfo.Copy(destDir);
35+
sourceDirInfo.Copy(destDir, true);
3636
Assert.True(Directory.Exists(destDir));
37+
38+
// 测试源文件夹不存在的情况
39+
var sourceDirNotExists = new DirectoryInfo(Path.Combine(rootDir, "notexists"));
40+
var ex = Assert.Throws<DirectoryNotFoundException>(() => sourceDirNotExists.Copy(destDir, true));
41+
Assert.NotNull(ex);
3742
}
3843

3944
private static string CreateDir(string dirName)

0 commit comments

Comments
 (0)