diff --git a/src/BootstrapBlazor/Extensions/DirectoryInfoExtensions.cs b/src/BootstrapBlazor/Extensions/DirectoryInfoExtensions.cs index 64663a4c3a9..03be03dcec3 100644 --- a/src/BootstrapBlazor/Extensions/DirectoryInfoExtensions.cs +++ b/src/BootstrapBlazor/Extensions/DirectoryInfoExtensions.cs @@ -11,29 +11,44 @@ namespace BootstrapBlazor.Components; public static class DirectoryInfoExtensions { /// - /// 文件夹拷贝方法 + /// Copies the contents of the current directory to a specified destination directory. /// - /// - /// - public static void Copy(this DirectoryInfo sourceDirInfo, string destDirName) + /// This method creates the destination directory if it does not already exist. Files in the + /// source directory are copied to the destination directory, and if is , all subdirectories and their contents are also copied recursively. + /// The source directory to copy from. + /// The path of the destination directory where the contents will be copied. + /// to copy all subdirectories and their contents recursively; otherwise, . + /// Thrown if the source directory specified by does not exist. + public static void Copy(this DirectoryInfo dir, string destinationDir, bool recursive = true) { - if (!Directory.Exists(destDirName)) + // Check if the source directory exists + if (!dir.Exists) { - Directory.CreateDirectory(destDirName); + throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}"); } - // CopyFile - foreach (var info in sourceDirInfo.EnumerateFileSystemInfos()) + // Create the destination directory + Directory.CreateDirectory(destinationDir); + + // Get the files in the source directory and copy to the destination directory + foreach (FileInfo file in dir.GetFiles()) { - if (info is FileInfo fi) - { - var targetFileName = Path.Combine(destDirName, info.Name); - fi.CopyTo(targetFileName, true); - } - else if (info is DirectoryInfo di) + string targetFilePath = Path.Combine(destinationDir, file.Name); + file.CopyTo(targetFilePath); + } + + // If recursive and copying subdirectories, recursively call this method + if (recursive) + { + // Cache directories before we start copying + DirectoryInfo[] dirs = dir.GetDirectories(); + + foreach (DirectoryInfo subDir in dirs) { - var targetFolderName = Path.Combine(destDirName, di.Name); - Copy(di, targetFolderName); + string newDestinationDir = Path.Combine(destinationDir, subDir.Name); + Copy(subDir, newDestinationDir, true); } } } diff --git a/test/UnitTest/Extensions/DirectoryInfoExtensionsTest.cs b/test/UnitTest/Extensions/DirectoryInfoExtensionsTest.cs index c3a072a9bd4..d36d577bebe 100644 --- a/test/UnitTest/Extensions/DirectoryInfoExtensionsTest.cs +++ b/test/UnitTest/Extensions/DirectoryInfoExtensionsTest.cs @@ -32,8 +32,13 @@ public void Copy_Ok() } var sourceDirInfo = new DirectoryInfo(sourceDir); - sourceDirInfo.Copy(destDir); + sourceDirInfo.Copy(destDir, true); Assert.True(Directory.Exists(destDir)); + + // 测试源文件夹不存在的情况 + var sourceDirNotExists = new DirectoryInfo(Path.Combine(rootDir, "notexists")); + var ex = Assert.Throws(() => sourceDirNotExists.Copy(destDir, true)); + Assert.NotNull(ex); } private static string CreateDir(string dirName)