예제 #1
0
        public static bool CopyDirectory(string SourcePath, string DestPath, bool Overwrite = false)
        {
            DirectoryInfo SourceDir = default(DirectoryInfo);
            DirectoryInfo DestDir   = default(DirectoryInfo);
            bool          bOk       = true;

            // the source directory must exist, otherwise throw an exception
            try
            {
                SourceDir = new DirectoryInfo(SourcePath);
                DestDir   = new DirectoryInfo(DestPath);
                if (SourceDir.Exists)
                {
                    // if destination SubDir's parent SubDir does not exist throw an exception
                    if (!DestDir.Parent.Exists)
                    {
                        throw new DirectoryNotFoundException("Destination directory does not exist: " + DestDir.Parent.FullName);
                    }

                    if (!DestDir.Exists)
                    {
                        DestDir.Create();
                    }
                    // copy all the files of the current directory
                    //FileInfo ChildFile = default(FileInfo);
                    foreach (FileInfo ChildFile in SourceDir.GetFiles())
                    {
                        if (Overwrite)
                        {
                            ChildFile.CopyTo(System.IO.Path.Combine(DestDir.FullName, ChildFile.Name), true);
                        }
                        else
                        {
                            // if Overwrite = false, copy the file only if it does not exist
                            if (!File.Exists(System.IO.Path.Combine(DestDir.FullName, ChildFile.Name)))
                            {
                                ChildFile.CopyTo(System.IO.Path.Combine(DestDir.FullName, ChildFile.Name), false);
                            }
                        }
                    }

                    // copy all the sub-directories by recursively calling this same routine
                    //DirectoryInfo SubDir = default(DirectoryInfo);
                    foreach (DirectoryInfo SubDir in SourceDir.GetDirectories())
                    {
                        CopyDirectory(SubDir.FullName, System.IO.Path.Combine(DestDir.FullName, SubDir.Name), Overwrite);
                    }
                }
                else
                {
                    throw new DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName);
                }
                bOk = true;
            }
            catch
            {
                bOk = false;
            }
            return(bOk);
        }
예제 #2
0
 static bool SafeDeleteDirectoryContents(string DirectoryName)
 {
     try
     {
         DirectoryInfo Directory = new DirectoryInfo(DirectoryName);
         foreach (FileInfo ChildFile in Directory.EnumerateFiles("*", SearchOption.AllDirectories))
         {
             ChildFile.Attributes = ChildFile.Attributes & ~FileAttributes.ReadOnly;
             ChildFile.Delete();
         }
         foreach (DirectoryInfo ChildDirectory in Directory.EnumerateDirectories())
         {
             SafeDeleteDirectory(ChildDirectory.FullName);
         }
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }