CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2009
    Location
    Liverpool, UK
    Posts
    5

    Problem concerning the File.GetLastWriteTime

    I seem to be getting very strange behaviour and it is obviously a result of my programming. What I was aiming for was an incremental backup software for a college project. The form being designed and a recursive file copying method I have done.

    Now to keep the form from freezing when the file copying operation is taking place I have used the backgroundWorker control in the graphical design view and I am using this to run the File Copying in a separate thread and that works.

    The FileCopying method looks like so:
    Code:
    public void FullCopy(string sConfigLocation, string sourceFolder, string destFolder, string sDriveName, object iniFile)
            {
                if (!Directory.Exists(destFolder))
                    Directory.CreateDirectory(destFolder);
            
                //System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
                string[] files = Directory.GetFiles(sourceFolder);
                foreach (string file in files)
                {
                    string name = Path.GetFileName(file);
                    string dest = Path.Combine(destFolder, name);
                    DateTime WriteTime = File.GetLastWriteTimeUtc(dest);
                    backgroundWorker1.ReportProgress(0, name);
                    try
                    {
                        File.Copy(file, dest, true);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("The file " + file + " could not be copied because of error, '" + ex + "'");
                    }
                    IniFunctions.WriteToIni(sConfigLocation, sDriveName, file, WriteTime.ToString(), iniFile); 
                }
                string[] folders = Directory.GetDirectories(sourceFolder);
                foreach (string folder in folders)
                {
                    string name = Path.GetFileName(folder);
                    string dest = Path.Combine(destFolder, name);
                    FullCopy10(sConfigLocation, folder, dest, sDriveName, iniFile);
                }
            }
    However when looking into the INI file all the times are, '01/01/1601 00:00:00'. I have no idea why. If I then call this after the method:

    Code:
     public void confirmTimeCheck(string sConfigLocation, string sourceFolder, string destFolder, string sDriveName, object iniFile)
            {
                string[] files = Directory.GetFiles(sourceFolder);
                foreach (string file in files)
                {
                    string name = Path.GetFileName(file);
                    string dest = Path.Combine(destFolder, name);
                    DateTime WriteTime = File.GetLastWriteTimeUtc(dest);
                    IniFunctions.WriteToIni(sConfigLocation, sDriveName, file, WriteTime.ToString(), iniFile);
                }
                string[] folders = Directory.GetDirectories(sourceFolder);
                foreach (string folder in folders)
                {
                    string name = Path.GetFileName(folder);
                    string dest = Path.Combine(destFolder, name);
                    confirmTimeCheck(sConfigLocation, folder, dest, sDriveName, iniFile);
                }
            }
    Which is just the exact same thing taking out the FileCopying operations it now works fine. The times are correct. :|

    So it seems that for some reason when I'm calling FileCopy operations in the same method as retrieving the timestamp, the timestamp will come back as, '01/01/1601 00:00:00' or as the same time the confirmTimeCheck method retrieves.

    So this is a problem now as I need to check the current LastWriteTime with one stored in the INI files so I can decide which ones I can update.

    Does anyone know why the File.GetLastWriteTime is not working properly in that method? Perhaps a problem with it being in a different thread? The backgroundWorker method is:

    Code:
    public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                CopyObject a = e.Argument as CopyObject;
                FullCopy(a.sConfigLocation, a.sourceFolder, a.destFolder, a.sDriveName, a.iniFile);
                confirmTimeCheck(a.sConfigLocation, a.sourceFolder, a.destFolder, a.sDriveName, a.iniFile)
                e.Result("Complete");
            }
    I hope someone can shed some light on this, thanks in advance.
    Last edited by AwAk3e; January 31st, 2010 at 04:13 PM.

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Problem concerning the File.GetLastWriteTime

    check the paths, msdn says that:
    File.GetLastWriteTimeUtc
    If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC).
    very strange. I would expect an exception and not such a strange result.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Oct 2009
    Location
    Liverpool, UK
    Posts
    5

    Re: Problem concerning the File.GetLastWriteTime

    .. Thank you for the hint on the paths. I can't believe I was so idiotic to put the DESTINATION of where the file is going to be. :| As I was giving it the destination it was checking where the file would be after the copy operation. :| That explains why it works the second time and not the first time.

    Very silly mistake yet I still would of missed it if it wasn't for you're Tip on the path . Thanks Memeloo, I appreciate it greatly. Thanks.

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

Featured