How to get file size for an ongoing file?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm writing to a file using StreamWriter.
I need to know after every file writing, the file size.
I do Flush after each line.

The FileInfo.Length should retune the file size, but it does not.

Here is what I'm doing:
StreamWriter myFile = File.AppendText(fileName);

myFile .Flush();
FileInfo fileInfo = new FileInfo(fileName);
.. . .
myFile.WriteLine("wrting some sext tp the file.");
myFile .Flush();
long size = m_FileInfo.Length;

But I get the same size every time !!!

my questions:
(1) Why I'm getting the same file size every time?
(2) If the write function failed to write the string or part of it, will it
tell be about it by exception or what?
 
Ok, I found the solution:

StreamWriter fWrite = File.CreateText("filename.txt");
fWrite.WriteLine("write somthing to the file");
// No need to call Flush() or set it to AutoFlush !!!
long theTrueFileSize = fWrite.BaseStream.Length;

That is it, simply use the BaseStream.Length


Peace
Sharon
 
Back
Top