AntDeployAgentWindows/Util/CopyHelper.cs
/// <summary>
/// copies source directory to the destionation directory
/// </summary>
/// <param name="srcDir">source directory</param>
/// <param name="dstDir">destination directory</param>
/// <param name="copySubDirs">copy sub directories</param>
copies source directory to the destionation directory
copies source directory to the destionation directory
public static void DirectoryCopy(string srcDir, string dstDir, bool copySubDirs)
        {
            DirectoryInfo dir = new DirectoryInfo(srcDir);
            if (!dir.Exists)
                throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + srcDir);
            if (!Directory.Exists(dstDir))
            {
                try
                {
                    Directory.CreateDirectory(dstDir);
                }
                catch (Exception ex)
                {
                    throw new Exception($"CreateDirectory:{dstDir} Fail:{ex.Message}");
                }
            }
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string dstFile = Path.Combine(dstDir, file.Name);
                try
                {
                    file.CopyTo(dstFile, true);
                }
                catch (Exception ex)
                {
                    throw new Exception($"from:{file.FullName} copy to {dstFile} Fail:{ex.Message}");
                }
            }
            if (copySubDirs)
            {
                DirectoryInfo[] subDirs = dir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirs)
                {
                    string dstDirSub = Path.Combine(dstDir, subDir.Name);
                    DirectoryCopy(subDir.FullName, dstDirSub, copySubDirs);
                }
            }
        }
public static void DirectoryCopy ( string srcDir , string dstDir , bool copySubDirs ) { DirectoryInfo dir = new DirectoryInfo ( srcDir ) ; if ( ! dir . Exists ) throw new DirectoryNotFoundException ( " " + srcDir ) ; if ( ! Directory . Exists ( dstDir ) ) { try { Directory . CreateDirectory ( dstDir ) ; } catch ( Exception ex ) { throw new Exception ( $" CreateDirectory: { dstDir }  Fail: { ex . Message } " ) ; } } FileInfo [ ] files = dir . GetFiles ( ) ; foreach ( FileInfo file in files ) { string dstFile = Path . Combine ( dstDir , file . Name ) ; try { file . CopyTo ( dstFile , true ) ; } catch ( Exception ex ) { throw new Exception ( $" from: { file . FullName }  copy to  { dstFile }  Fail: { ex . Message } " ) ; } } if ( copySubDirs ) { DirectoryInfo [ ] subDirs = dir . GetDirectories ( ) ; foreach ( DirectoryInfo subDir in subDirs ) { string dstDirSub = Path . Combine ( dstDir , subDir . Name ) ; DirectoryCopy ( subDir . FullName , dstDirSub , copySubDirs ) ; } } }
copies source directory to the destionation directory
copies source directory to the destionation directory
srcDir
string
None
dstDir
string
None
copySubDirs
bool
copy sub directories
copy sub directories