コード例 #1
0
        private void CopyModifiedDirectory(string path, string subPath)
        {
            DirectoryInfo diSource = new DirectoryInfo(Path.Combine(path, subPath));

            if (!String.IsNullOrEmpty(subPath))
            {
                //sbLog.Append ((++count) + "," + Path.Combine (sourceOriginal, diSource.Name) + "," + diSource.LastWriteTime + Environment.NewLine);
                String newDir = Path.Combine(destOriginal, subPath);
                Directory.CreateDirectory(newDir);
                Directory.SetCreationTime(newDir, Directory.GetLastWriteTime(diSource.FullName));
                //Directory.SetLastWriteTime(newDir, DateTime.Now);
            }

            foreach (FileInfo f in diSource.GetFiles())
            {
                String destFile = Path.Combine(destOriginal, subPath, f.Name);
                try {
                    if (!mDictOldBackup.ContainsKey(Path.Combine(subPath, f.Name)))
                    {
                        //sbLog.Append ((++count) + "," + Path.Combine (fi.FullName) + "," + fi.LastWriteTime +  Environment.NewLine);
                        File.Copy(f.FullName, destFile, false);
                        //File.SetLastWriteTime (newFile, DateTime.Now);
                    }
                    else
                    {
                        if (isOverwritable && f.LastWriteTime != File.GetLastAccessTime(destFile))
                        {
                            File.Copy(f.FullName, destFile, true);
                            //File.SetLastWriteTime (newFile, DateTime.Now);
                        }
                    }
                } catch (Exception e) {
                    mErrorForm = new ErrorNotifier();
                    mErrorForm.SetSourcePath(f.FullName);
                    mErrorForm.SetDestPath(destFile);
                    mErrorForm.SetErrorMsg(e.StackTrace);
                    mErrorForm.SetRenameText(Path.GetFileNameWithoutExtension(f.Name), f.Extension);
                    mErrorForm.ShowDialog();

                    if (ErrorNotifier.EResult == ErrorNotifier.drResult.RENAME)
                    {
                        destFile = Path.Combine(destOriginal, ErrorNotifier.NewName);
                        File.Copy(f.FullName, destFile, true);
                        File.SetLastWriteTime(destFile, DateTime.Now);
                    }

                    Console.WriteLine("ERROR: " + e.StackTrace);
                    isSuccess = false;
                }
            }

            foreach (DirectoryInfo diSourceSubDir in diSource.GetDirectories())
            {
                CopyModifiedDirectory(path, Path.Combine(subPath, diSourceSubDir.Name));
            }

            isSuccess = true;
        }
コード例 #2
0
        private void CopyDirectory(string path, string subPath)
        {
            // Cancels copy by skipping all remaining items
            if (ErrorNotifier.EResult == ErrorNotifier.drResult.CANCEL)
            {
                return;
            }

            DirectoryInfo src = new DirectoryInfo(path);

            foreach (DirectoryInfo d in src.GetDirectories())
            {
                CopyDirectory(d.FullName, Path.Combine(subPath, d.Name));
            }

            DirectoryInfo dest = new DirectoryInfo(Path.Combine(destOriginal, subPath));

            if (!Directory.Exists(dest.FullName) && dest.GetDirectories().Count() != 0)
            {
                Directory.CreateDirectory(dest.FullName);
            }
            foreach (FileInfo f in src.GetFiles())
            {
                string destFile = Path.Combine(dest.FullName, f.Name);
                try {
                    // Cancels copy by skipping all remaining items
                    if (ErrorNotifier.EResult == ErrorNotifier.drResult.CANCEL)
                    {
                        return;
                    }

                    if (!File.Exists(destFile))
                    {
                        //throw new Exception ();
                        mLblCopyingText.Text = f.FullName + "\n to \n" + destFile;
                        File.Copy(f.FullName, destFile, false);
                        File.SetLastWriteTime(destFile, DateTime.Now);
                    }
                    else
                    {
                        if (isOverwritable && f.LastWriteTime != File.GetLastAccessTime(destFile))
                        {
                            //throw new Exception ();
                            mLblCopyingText.Text = f.FullName + "\n to \n" + destFile;
                            File.Copy(f.FullName, destFile, true);
                            File.SetLastWriteTime(destFile, DateTime.Now);
                        }
                    }
                } catch (Exception e) {
                    mErrorForm = new ErrorNotifier();
                    mErrorForm.SetSourcePath(f.FullName);
                    mErrorForm.SetDestPath(destFile);
                    mErrorForm.SetErrorMsg(e.StackTrace);
                    mErrorForm.SetRenameText(Path.GetFileNameWithoutExtension(f.Name), f.Extension);
                    mErrorForm.ShowDialog();

                    if (ErrorNotifier.EResult == ErrorNotifier.drResult.RENAME)
                    {
                        destFile = Path.Combine(dest.FullName, ErrorNotifier.NewName);
                        File.Copy(f.FullName, destFile, true);
                        File.SetLastWriteTime(destFile, DateTime.Now);
                    }

                    Console.WriteLine("ERROR: " + e.StackTrace);
                    isSuccess = false;
                }
            }
            isSuccess = true;
        }