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

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

Patrik Svensson
  • 13,536
  • 8
  • 56
  • 77
Moh Moh Oo
  • 269
  • 1
  • 3
  • 19

5 Answers5

26

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
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
3

I think what you are looking for if you need properties of a specific file is FileInfo.

FileInfo info = new FileInfo(fullFilePath);
//Gets the size, in bytes, of the current file.
long size = info.Length;
Bearcat9425
  • 1,580
  • 1
  • 11
  • 12
1

Here you go! Just use the FileInfo class in the System.IO namespace :)

using System.IO;

var fullFilePath = "C:\path\to\some\file.txt";
var fileInfo = new FileInfo(fullFilePath);

Console.WriteLine("The size of the file is: " + fileInfo.Length);
Maritim
  • 2,111
  • 4
  • 29
  • 59
  • 1
    basically correct but better show in context with the `using(){}` – H H Aug 08 '13 at 13:42
  • That is not needed. It has nothing to do with the instantiation of the StreamWriter class. It is only (naturally) required that this is done within the scope of where fullFilePath is defined and declared. Whether or not the file size is checked before or after writing the content is up to the original poster to decide. But he or she should be perfectly capable of utilizing this code no matter what they intend to do :) – Maritim Aug 08 '13 at 13:43
  • FileInfo doesn't implement IDisposable therefore a using statement cant be used on it. – Bearcat9425 Aug 08 '13 at 13:45
  • What is the relevance @Bearcat9425? Just instantiate FileInfo after or before writing your file, depending on the behaviour you want, and then set the object to null once you're done. The GC will take care of the rest. – Maritim Aug 08 '13 at 13:45
  • 1
    To the person above stating to show it in context with the using. – Bearcat9425 Aug 08 '13 at 13:46
  • It's relevant to place this outside and after the `using(writer)`. – H H Aug 08 '13 at 13:50
  • The OP should be able to figure that out. – Maritim Aug 08 '13 at 13:51
  • FileInfo caches, so @HenkHolterman is correct that it must be after the writer completes. Or you need to call Refresh() – bland Aug 08 '13 at 13:51
  • _The OP should be able to figure that out_ - maybe, it still makes it only a half-answer. – H H Aug 08 '13 at 13:53
  • I just tested this. The above code return 0. I have to put `writer.AutoFlush = true;` before creating FileInfo object. I tested this within using block. – Moh Moh Oo Aug 08 '13 at 13:57
  • @mohmohoo You have to actually write the data to the file in the file system before the file size increases. That's IO. – Maritim Aug 08 '13 at 14:06
  • @mohmohoo - inside the using the BaseStream approach is probably faster. The need to call `Flush()` remains. – H H Aug 08 '13 at 15:51
0

Could you try this piece of code ?

var size = writer.BaseStream.Length;

*writer is your StreamWriter

Etienne Arthur
  • 680
  • 1
  • 9
  • 17
0

No, not from StreamWriter itself can you find out about the file's size. You have to use FileInfo's Length.

bland
  • 1,968
  • 1
  • 15
  • 22