@@ -11,29 +11,44 @@ namespace BootstrapBlazor.Components;
1111public  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    } 
0 commit comments