Re: Calling FileStream.Create after File.Move gets old file handle
- From: "Damien" <Damien_The_Unbeliever@xxxxxxxxxxx>
- Date: 12 Jul 2006 03:26:15 -0700
C# Dev wrote:
"C# Dev" wrote:I've managed to repeat this, using VB.Net. I believe it may be a
Greetings all,Anyone?
Have a question regarding File.Move / FileStreams
My steps,
1. Create file1 demo.log [FileMode.CreateNew]
2. Move file to demo1.log
3. Create file2 with same name as in #1: demo.log [FileMode.CreateNew]
Now, problem is that the filehandle in #1 and #3 are the same.
Are they expected to be?
I suspect this is the reason why file2 inherits the CreateDate from file1
which is my main problem :-/
Can anyone please enlighten me what is going on?
Cheers
If this can be replicated it seems like a bug to me..
windows filesystem "feature". I cannot find a reference for it, but I
believe it's been done like this to support scenarios where an
application is about to write out an updated copy of a file, but wants
to support being able to retrieve the "old" version, if there's a
problem during the write.
Obviously, you cannot overwrite the existing file, so you have to
rename it first, and then write out the new version using the old
filename. And then windows (I believe. Might be .Net, but I suspect
windows) steps in and says "Aha! This is obviously just an updated copy
of the file that's just been moved, it should inherit some meta-data,
such as the creation date (and permissions)". I think it's done on a
per-process basis (so if one process renames a file, and another writes
the new one, it will not inherit the meta data).
Now, final two bits. First, they have the same handle because the first
free handle is obtained when you open the file, and then is freed by
the Close() call, so that when you come to open the second file, it
finds the first free handle (which happens to be the same handle
previously used). Had you reopened the moved file (for reading say),
before you had created the second file, you'd have probably had
different handle values.
Secondly, if you need the new file to have the correct creation
datetime, I think you'll need to open it twice. Create it, Close it,
Call SetCreationTime, then open it for writing.
Damien
.
- Follow-Ups:
- References:
- Prev by Date: Re: Custom error message
- Next by Date: Data Reader Problem
- Previous by thread: RE: Calling FileStream.Create after File.Move gets old file handle
- Next by thread: Re: Calling FileStream.Create after File.Move gets old file handle
- Index(es):
Relevant Pages
|