1

I am running some basic unit test on the creation date of files and I runned in a strange case (to me at least):

    [TestInitialize]
    public void Initialize()
    {
        if (File.Exists(UncFile))
            File.Delete(UncFile);
        if (File.Exists(LocalFile))
            File.Delete(LocalFile);
    }

    [TestMethod]
    public void ProxyFile_DeleteOlderFileOnLocalSystem()
    {
        using (StreamWriter sw = File.CreateText(LocalFile)) { }
        Thread.Sleep(50);

        using (StreamWriter sw = File.CreateText(UncFile)) { }
        Thread.Sleep(50);

        DateTime UncDate = File.GetCreationTime(UncFile);
        DateTime OldLocalDate = File.GetCreationTime(LocalFile);

        Assert.IsTrue(UncDate > OldLocalDate);
    }

Works fine! whereas :

    [TestInitialize]
    public void Initialize()
    {
        using (StreamWriter sw = File.CreateText(UncFile)) { }

        if (File.Exists(UncFile))
            File.Delete(UncFile);
        if (File.Exists(LocalFile))
            File.Delete(LocalFile);
    }


    [TestMethod]
    public void ProxyFile_DeleteOlderFileOnLocalSystem()
    {
        using (StreamWriter sw = File.CreateText(LocalFile)) { }
        Thread.Sleep(50);

        using (StreamWriter sw = File.CreateText(UncFile)) { }
        Thread.Sleep(50);

        DateTime UncDate = File.GetCreationTime(UncFile);
        DateTime OldLocalDate = File.GetCreationTime(LocalFile);

        Assert.IsTrue(UncDate > OldLocalDate);
    }

gaves me false... The Only Difference between the two being :

using (StreamWriter sw = File.CreateText(UncFile)) { }

But I delete this file just after :

if (File.Exists(UncFile))
                File.Delete(UncFile);

Could someone point me out what I am not doing right?

thx.

[EDIT]

the date in the second example seems to be like the first file UNC Created :

Debug.Print("UncFile : " + File.GetCreationTime(UncFile).Ticks);
->
UncFile   (Init) : 634632802355468953
UncFile   (Test) : 634632802355468953
LocalFile (Test) : 634632802355618962

[/EDIT]

5
  • It seems like if the creation time of the first UncFile is the same as the creation time of the second UncFile, maybe UncFile is not being deleted in Initialize()?
    – Brian Snow
    Jan 27, 2012 at 16:06
  • Try to debug your test step by step and look at the files while doing this and I'm sure you'll figure out what's going on. Jan 27, 2012 at 16:13
  • 3
  • the files are correctly deleted, I have debugged it. The problem is to update the native functions responsible to give the Creation date. trying with FileInfo fiUnc = new FileInfo(UncFile); fiUnc.Refresh(); DateTime UncDate = fiUnc.CreationTime; But does not work so far..
    – Arthis
    Jan 27, 2012 at 16:23
  • @Raymond exactly.. thanks fot the link!
    – Arthis
    Jan 27, 2012 at 16:24

1 Answer 1

1

OK found it :

File.GetCreationTime Method

Note This method may return an inaccurate value, because it uses native functions whose values may not be continuously updated by the operating system.

Thanks to raymond link's to another answer in Stackoverflow , Here is the solution :

 using (StreamWriter sw = File.CreateText(LocalFile)) { }
            File.SetCreationTime(LocalFile,DateTime.Now);
            Thread.Sleep(50);

            using (StreamWriter sw = File.CreateText(UncFile)) { }
            File.SetCreationTime(UncFile, DateTime.Now);
            Thread.Sleep(50);

            DateTime UncDate = File.GetCreationTime(UncFile);
            DateTime OldLocalDate = File.GetCreationTime(LocalFile);

            Assert.IsTrue(UncDate > OldLocalDate);
2
  • Yeah, I was going to say something along these lines. If you delete a file and create a new one of the same name very quickly it will retain some of the attributes from the old one I believe (it used to at least). I'm not sure if its just a case of not being updated by the system as it is a deliberate feature... I'd certainly be interested to see if you did that with an old already existing file whether the new file retains the old creation date or if it does eventually update... Looks like +Raymond Chen already covered this in a comment above. And better. ;-)
    – Chris
    Jan 27, 2012 at 16:34
  • Thanks for the explanation and providing a solution. Well done.
    – J Pollack
    Jul 5, 2012 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.