XCopy命令用于归档具有原始日期戳的文件

时间:2013-08-19 23:50:49

标签: c# .net xcopy

我正在尝试编写一个.NET控制台应用程序,该应用程序将使用xcopy复制超过x天的文件,同时保持原始日期创建的时间戳。我目前有这个作为我的命令:

     /// <summary>
    /// Performs Copy and Verification using xcopy
    /// </summary>
    /// <returns>true if success, false otherwise</returns>
    internal bool CopyAndVerify()
    {
        string date = @"d:" + time.ToString("MM/dd/yyyy");
        date = date.Replace('/', '-');
        date = date.Insert(0, "/");
        Process exeProcess = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardError = true;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.FileName = "xcopy.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = "\"" + source + "\"" + " " + "\"" + dest + "\"" + @" /v " + date + " /i /s /r /h /y /z";
        try
        {
            using (exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

代码执行复制和验证,但是当我测试时,我发现修改了文件夹/子文件夹日期,创建日期是复制的时间。我做错了什么?

1 个答案:

答案 0 :(得分:2)

这是一些简单的代码,您可以在xcopy之后运行,将子目标文件夹的日期和时间设置为与源相同。希望这是有帮助的。



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    namespace Copy
    {
        class CopyDirTimestamps
        {
            public static bool CopyTimestamps(
                string sourceDirName, string destDirName, bool copySubDirs)
            {
                try
                {
                    CopyForDir(sourceDirName, destDirName, copySubDirs, false);
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }


            private static void CopyForDir(
                string sourceDirName, string destDirName, bool copySubDirs, bool isSubDir)
            {
                DirectoryInfo dir = new DirectoryInfo(sourceDirName);
                DirectoryInfo[] dirs = dir.GetDirectories();

                // If the source directory does not exist, throw an exception.
                if (!dir.Exists)
                {
                    throw new DirectoryNotFoundException(
                        "Source directory does not exist or could not be found: "
                        + sourceDirName);
                }

                if (!Directory.Exists(destDirName)) return;

                DirectoryInfo destDir = new DirectoryInfo(destDirName);

                // If copySubDirs is true, copy the subdirectories.
                if (copySubDirs)
                {
                    foreach (DirectoryInfo subdir in dirs)
                    {
                        // Create the subdirectory.
                        string temppath = Path.Combine(destDirName, subdir.Name);

                        // Copy the subdirectories.
                        CopyForDir(subdir.FullName, temppath, copySubDirs, true);
                    }
                }

                if (isSubDir)
                {
                    destDir.CreationTime = dir.CreationTime;
                    destDir.LastAccessTime = dir.LastAccessTime;
                    destDir.LastWriteTime = dir.LastWriteTime;
                }
            }
        }
    }