File.Delete(filepath) followed by new FileStream(filepath,FileMode.Create) not updating file's creation timestamp RRS feed

  • Question

  • I'm scratching my head over this one. Particularly since it seems it would have been discovered before now...

    Using Win7Pro with all updates applied.  I have an app that at one point performs the following pair of .NET calls:

    File.Delete(path: filePath);
    fileStream = new FileStream(path: filePath, mode: FileMode.Create, access: FileAccess.Write, share: FileShare.Read);

    Those two lines are literally one right after the other.  What I'm seeing in File Explorer is the creation timestamp on the file does not get changed if filePath already exists.  In fact I can step through the code and after executing the File.Delete(), File Explorer shows the file is gone.  After executing the "new FileStream()", File Explorer shows the file has reappeared.  However File Explorer reports that the file has the old original creation timestamp.  This includes when checking "right click > Properties" on the file.

    I can first create the file, then wait 5 minutes before executing the above.  When stepping through the code, File Explorer will show the file disappear in response to File.Delete(), and then reappear in response to "new FileStream()".  Then "right click > Properties" on the file will report the file was created "5 minutes ago".

    Originally I did not have the File.Delete() in the code because it seemed like FileMode.Create should have done what I wanted -- eliminate the old file and create a new one.  When that didn't result in the creation timestamp updating, I then tried the separate File.Delete() since that seemed more explicit.  That didn't work either.

    The "modified" and "accessed" timestamps on the file DO update.

    This actually hints at some kind of Win7 caching/reuse error.  In most cases it would probably go unnoticed.

    Can anyone replicate this?

    -- kburgoyne

    Thursday, November 27, 2014 1:12 AM

All replies

  • >>Using Win7Pro with all updates applied

    Do you mean you only repro on Win7pro?

    If you put File.Delete(path: filePath)  back ? What happens

    Friday, November 28, 2014 7:33 AM
  • Doesn't matter.  With or without File.Delete(), the file's creation timestamp isn't changing.

    -- kburgoyne

    Saturday, November 29, 2014 5:12 PM
  • Make sure you don't reuse any objects like a stream.  Dipose the stream you are using to make sure old data gets destroyed. 


    Saturday, November 29, 2014 6:25 PM
  • The stream had not been previously in use.  This is the first access to the file (or any file) during the execution of the program.

    -- kburgoyne

    Saturday, November 29, 2014 6:57 PM