Connecting Tech Pros Worldwide Help | Site Map

How to get file size for an ongoing file?

Sharon
Guest
 
Posts: n/a
#1: Nov 16 '05
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?

--
Thanks
Sharon G.
Sharon
Guest
 
Posts: n/a
#2: Nov 16 '05

re: How to get file size for an ongoing file?


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
Closed Thread