Вопрос

I am trying to write a .NET console app that will use xcopy to copy over files newer than x days, while maintaining the original date created timestamp. I currently have this as my command:

     /// <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;
    }

The code performs the copy and verification, but then when I test I find that the Folders/SubFolders date modified and date created is the time of the copy. What am I doing wrong?

Это было полезно?

Решение

Here's some simple code you could run after the xcopy to set the subdir destination folder dates and times to the same as the source. Hope this is helpful.



    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;
                }
            }
        }
    }


Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top