How to get file size for an ongoing file?

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?
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top