Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the file size from StreamWriter [closed]

using (var writer = File.CreateText(fullFilePath))
{
   file.Write(fileContent);
}

Given the above code, can the file size be known from StreamWriter?

like image 867
Moh Moh Oo Avatar asked Aug 08 '13 13:08

Moh Moh Oo


People also ask

What is the difference between FileStream and StreamWriter?

Specifically, a FileStream exists to perform reads and writes to the file system. Most streams are pretty low-level in their usage, and deal with data as bytes. A StreamWriter is a wrapper for a Stream that simplifies using that stream to output plain text.

What is StreamReader and StreamWriter?

The StreamReader and StreamWriter classes are used for reading from and writing data to text files. These classes inherit from the abstract base class Stream, which supports reading and writing bytes into a file stream.

Where does StreamWriter write to?

StreamWriter. WriteLine() method writes a string to the next line to the steam. The following code snippet creates and writes different author names to the stream.


1 Answers

Yes, you can, try the following

long length = writer.BaseStream.Length;//will give unexpected output if autoflush is false and write has been called just before

Note: writer.BaseStream.Length property can return unexpected results since StreamWriter doesn't write immediately. It caches so to get expected output you need AutoFlush = true

writer.AutoFlush = true; or writer.Flush();
long length = writer.BaseStream.Length;//will give expected output
like image 101
Sriram Sakthivel Avatar answered Oct 27 '22 00:10

Sriram Sakthivel