Results 1 to 6 of 6

Thread: File Date & Time on Copy

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    53

    File Date & Time on Copy

    I'm copying a file:

    File.Copy("File1","File2")

    When copied, the file will have it's file date and time preserved. How can I make the newely copied file take on the date and time at that moment of copy?

    Thanks

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    System.IO.File has members which include SetCreationTime, SetLastAccessTime and SetLastWriteTime.

    Haven't tried it but it looks promising.

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Try this:

    Code:
    using System;
    using System.IO;
    
    class MyFile
    {
        static void FileCopy(string source, string destination, bool overwrite, bool changeTimestamp)
        {
            System.IO.File.Copy(source, destination, overwrite);
    	if(changeTimestamp)
    	{
    	    File.SetCreationTime(destination, DateTime.Now);
    	}
        }
    
        static void Main()
        {
            MyFile.FileCopy(@"d:\dummy.txt", @"d:\dummyCopy.txt", true, true);
        }
    }

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    53
    The above is C++ ..................................

    anyway, almost identical, the SetCreationTime didn't work as the date was the same.

    The only way I've found to do it now is like this, which works:

    Code:
        Dim fi As New FileInfo(MyFile)
        Dim aDate As DateTime = (Date.Now)
        fi.CreationTime = aDate
        fi.LastAccessTime = aDate
        fi.LastWriteTime = aDate
    Works OK but a bit 'long winded' if you ask me.

    Thanks

  5. #5
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Actually, it's C#.....................

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    53
    lol, that's what I meant.... it was late.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width