Re: FileStream.Flush not flushing?

Tech-Archive recommends: Speed Up your PC by fixing your registry



Hmmm. Very interesting.
So when does the OS buffer get updated normally when using a
FileStream? Is it totally up to the OS to decide when to do this?

It seems odd that I would have to bypass .NET to get the behaviour I
want. So this suggests to me that I am using it incorrectly, or I have
a problem understanding the nature of streams. Is it an understood
feature of FileStreams that you cannot guarantee expect the underlying
file to be "correct" until after you close the stream? Is this true of
the full framework / Win32 as well? I always thought Flush wrote the
contents of the buffer to the underlying device - in this case a
file....

Also, as an interesting aside, if I carry out the same experiments on
a file in RAM (just /testfile.dat, not /Mounted Volume/testfile.dat)
the file size is "correct" without closing the stream.


Sergey Bogdanov <sergey.bogdanov@xxxxxxxxx> wrote in message news:<Om1n0caOFHA.3828@xxxxxxxxxxxxxxxxxxxx>...
> In fact, it flushes only internal buffer but does not touch OS buffer.
> To flush OS buffer you must use FlushFileBuffers function. Due to the
> fact that FileStream does not use directly CreateFile/WriteFile and
> private _handle is incorrect for FlushFileBuffers [1] you have to
> implement you own implementation of file stream (that uses CreateFile,
> WriteFile, etc.) or just close a stream every time.
>
> [1]
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceobjst/html/cerefFlushFileBuffers.asp
>
> Best regards,
> Sergey Bogdanov
> http://www.sergeybogdanov.com
>
>
> Andrew Revell wrote:
> > I am writing an applicaiton on the following platform:
> >
> > Advantech 133Mhz PCM3348.
> > Windows CE 5.0
> > Compact Framework.
> >
> > As part of the application, I am writing information to file (that is,
> > compact flash) with the expressed intention of protecting it from
> > unexpected power failures etc.
> >
> > What I am finding is that FileStream acts very strangely.....
> >
> > The basic symptom is that sometimes after writing data to the stream
> > and flushing it, the file size is wrong... eg this works:
> >
> > fstream.Write(record, 0, record.Length);
> > fstream.Write(record, 0, record.Length);
> > fstream.Flush();
> >
> > (the size of the file is OK - record.Length * 2)
> >
> > However if I do this:
> >
> > fstream.Write(record, 0, record.Length);
> > fstream.Seek(0, SeekOrigin.Current);
> > fstream.Write(record, 0, record.Length);
> > fstream.Flush();
> >
> > or even (!?!?):
> >
> > fstream.Write(record, 0, record.Length);
> > fstream.Flush();
> > fstream.Write(record, 0, record.Length);
> > fstream.Flush();
> >
> > the size of the file is only record.Length! WTF?!?
> > If I close the stream first (fstream.Close()), then it is fine - the
> > file size if correct.
> >
> > BTW I measure the size of the file by either using FileInfo, or
> > rebooting and checking the size of the file. Also these results are
> > very repeatable - it works the same every time.
> >
> > This has got me very confused. I don't want to close the stream every
> > time I write to it. That is expensive!
> >
> > Any help glady received.
.



Relevant Pages

  • Re: I/O buffering
    ... classes (those with word Buffer in their name)? ... Stream class. ... One would not normally use BufferedStream with a FileStream, ... explicitly dispose your custom Stream class, when you were sure you were   ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: I/O buffering
    ... StreamReader also buffers data? ... so they buffer as well. ... Neither StreamReader nor StreamWriter inherit from FileStream. ... What StreamReader and StreamWriter _do_ use is any Stream instance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: BinaryWriter (or streams) slow performance
    ... Who says you're the only one writing to that stream? ... The stream has the Position right. ... and then seeking back up to fill in the block size which it ... writer buffer, it could seek back to it, modify it in memory, and there's ...
    (microsoft.public.dotnet.framework.performance)
  • Re: I/O buffering
    ... So essentially, FileStream is buffered class?! ... mdo> From experience, not always. ... Specifically, if the FileStream.Dispose method gets called by the runtime, the buffer will be flushed and the file closed. ... mdo> No. Flushonly applies to writing the stream to disk. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Read and write to ascii file at the same time?
    ... StreamWriter from a file stream, but these are mutually exclusive meaning ... Then writing that file ... Or, if you are not shy of using bytes, you could work with the FileStream ... > Is it possible to open an ascii file, read the first 499 lines, overwrite ...
    (microsoft.public.dotnet.languages.csharp)