Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions src/BootstrapBlazor/Extensions/DirectoryInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,44 @@ namespace BootstrapBlazor.Components;
public static class DirectoryInfoExtensions
{
/// <summary>
/// 文件夹拷贝方法
/// Copies the contents of the current directory to a specified destination directory.
/// </summary>
/// <param name="sourceDirInfo"></param>
/// <param name="destDirName"></param>
public static void Copy(this DirectoryInfo sourceDirInfo, string destDirName)
/// <remarks>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 <paramref name="recursive"/> is <see
/// langword="true"/>, all subdirectories and their contents are also copied recursively.</remarks>
/// <param name="dir">The source directory to copy from.</param>
/// <param name="destinationDir">The path of the destination directory where the contents will be copied.</param>
/// <param name="recursive"><see langword="true"/> to copy all subdirectories and their contents recursively; otherwise, <see
/// langword="false"/>.</param>
/// <exception cref="DirectoryNotFoundException">Thrown if the source directory specified by <paramref name="dir"/> does not exist.</exception>
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): File.CopyTo does not overwrite existing files, which may cause issues if destination files already exist.

The previous code allowed overwriting by passing 'true' to 'CopyTo'. To maintain this behavior and prevent exceptions when files exist, add 'true' as the second argument.

}

// 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);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion test/UnitTest/Extensions/DirectoryInfoExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DirectoryNotFoundException>(() => sourceDirNotExists.Copy(destDir, true));
Assert.NotNull(ex);
Comment on lines +35 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Missing test for non-recursive copy (recursive = false).

Please add a test with 'recursive' set to false to confirm subdirectories are not copied when recursion is disabled.

Suggested change
sourceDirInfo.Copy(destDir, true);
Assert.True(Directory.Exists(destDir));
// 测试源文件夹不存在的情况
var sourceDirNotExists = new DirectoryInfo(Path.Combine(rootDir, "notexists"));
var ex = Assert.Throws<DirectoryNotFoundException>(() => sourceDirNotExists.Copy(destDir, true));
Assert.NotNull(ex);
sourceDirInfo.Copy(destDir, true);
Assert.True(Directory.Exists(destDir));
// Test non-recursive copy (recursive = false)
var nonRecursiveDestDir = Path.Combine(rootDir, "nonRecursiveDest");
sourceDirInfo.Copy(nonRecursiveDestDir, false);
Assert.True(Directory.Exists(nonRecursiveDestDir));
// Check that files in the root of sourceDir are copied
foreach (var file in Directory.GetFiles(sourceDir))
{
var fileName = Path.GetFileName(file);
Assert.True(File.Exists(Path.Combine(nonRecursiveDestDir, fileName)));
}
// Check that subdirectories are NOT copied
foreach (var subDir in Directory.GetDirectories(sourceDir))
{
var subDirName = Path.GetFileName(subDir);
Assert.False(Directory.Exists(Path.Combine(nonRecursiveDestDir, subDirName)));
}
// 测试源文件夹不存在的情况
var sourceDirNotExists = new DirectoryInfo(Path.Combine(rootDir, "notexists"));
var ex = Assert.Throws<DirectoryNotFoundException>(() => sourceDirNotExists.Copy(destDir, true));
Assert.NotNull(ex);

}

private static string CreateDir(string dirName)
Expand Down
Loading