Exemple #1
0
        ///
        /// <summary>
        /// Copy a directory, including all files and [optionally] subdirectories.
        /// </summary>
        /// <param name="InName">Input directory name.</param>
        /// <param name="OutName">Output directory name.</param>
        /// <param name="IncludeDirs">true to include subdirectories.</param>
        /// <param name="CheckAttr">true to reset attributes of existing files before copying
        /// (allows r/o files to be overwritten).</param>
        /// <param name="PreserveTime">true to preserve the timestamp from the input file.</param>
        /// <param name="OnlyNew">True to only copy new files (no existing file).</param>
        /// <param name="KeepNewest">true to retain any newer existing file.</param>
        /// <param name="ErrorHandler">Error handler for a file copy failure.</param>
        /// <returns>error message if copy failed, otherwise null.</returns>
        ///
        public static string CopyDirectory(string InName, string OutName,
                                           bool IncludeDirs, bool CheckAttr, bool PreserveTime,
                                           bool OnlyNew, bool KeepNewest, FileErrorHandler ErrorHandler)
        {
            if (!Directory.Exists(InName))
            {
                return("Directory " + InName + " does not exist!");
            }

            try
            {
                DirectoryInfo Input = new DirectoryInfo(InName);
                if (!Directory.Exists(OutName))
                {
                    Directory.CreateDirectory(OutName);
                }

                if (IncludeDirs)
                {
                    DirectoryInfo[] SubDirs = Input.GetDirectories();
                    foreach (DirectoryInfo DirName in SubDirs)
                    {
                        string ErrMsg = CopyDirectory(Path.Combine(InName, DirName.Name),
                                                      Path.Combine(OutName, DirName.Name), true,
                                                      CheckAttr, PreserveTime, false, ErrorHandler);
                        if (ErrMsg != null)
                        {
                            return(ErrMsg);
                        }
                    }
                }

                FileInfo[] InFiles = Input.GetFiles();
                foreach (FileInfo InFile in InFiles)
                {
                    string FileName = Path.Combine(OutName, InFile.Name);

                    if (File.Exists(FileName))
                    {
                        if (OnlyNew)
                        {
                            continue;
                        }
                        if (KeepNewest)
                        {
                            DateTime OldFileTime = DataConverter.ReadDateToSecond(File.GetLastWriteTimeUtc(FileName));
                            DateTime NewFileTime = DataConverter.ReadDateToSecond(InFile.LastWriteTimeUtc);
                            if (OldFileTime > NewFileTime)
                            {
                                continue;
                            }
                        }
                        if (CheckAttr)
                        {
                            File.SetAttributes(FileName, FileAttributes.Normal);
                        }
                    }

                    // Make sure not Hidden
                    FileAttributes TempAtt = InFile.Attributes;
                    if ((TempAtt & FileAttributes.Hidden) != FileAttributes.Hidden)
                    {
                        bool Retry  = true;
                        bool Copied = false;
                        while (Retry)
                        {
                            try
                            {
                                Retry = false;
                                InFile.CopyTo(FileName, true);
                                Copied = true;
                            }
                            catch (Exception ex)
                            {
                                if (ErrorHandler == null)
                                {
                                    throw;
                                }

                                FileErrorEvent  Ev     = new FileErrorEvent(InFile, FileName, ex);
                                FileErrorAction Action = ErrorHandler(Ev);
                                switch (Action)
                                {
                                case FileErrorAction.Cancel:
                                    return(Ev.ReturnMsg);

                                case FileErrorAction.Retry:
                                    Retry = true;
                                    break;

                                default:
                                    break;
                                }
                            }
                        }

                        if (PreserveTime && Copied)
                        {
                            FileAttributes NewAttr = 0;
                            if (InFile.IsReadOnly)
                            {
                                NewAttr  = File.GetAttributes(FileName);
                                NewAttr &= ~FileAttributes.ReadOnly;
                                File.SetAttributes(FileName, NewAttr);
                            }
                            File.SetCreationTimeUtc(FileName, InFile.CreationTimeUtc);
                            File.SetLastAccessTimeUtc(FileName, InFile.LastAccessTimeUtc);
                            File.SetLastWriteTimeUtc(FileName, InFile.LastWriteTimeUtc);
                            if (InFile.IsReadOnly)
                            {
                                NewAttr |= FileAttributes.ReadOnly;
                                File.SetAttributes(FileName, NewAttr);
                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                return(ex.Message);
            }

            return(null);
        }