Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's a good way to get file size?

Tags:

c#

.net

In our customized C# logging system, we use streamWriter = File.CreateText(fileNameStr); to create a file and open a stream for write.

Now we want to monitor the file size to see if it reach the max required size. What I did is the following:

  1. create a FileInfo object for about file: currFileInfo = new FileInfo(fileNameStr);
  2. get file size after each write: curFileInfo.Refresh(); fileSize = curFileInfo.Length;
  3. compare the file size with max file size, if bigger, close the current one and create a new file.

I have print out to see how long it will take to refresh the FileInfo, many times it will take about 15msec.

So I am thinking there may be a better way to do this. what's your suggestion?

like image 260
5YrsLaterDBA Avatar asked Oct 22 '10 13:10

5YrsLaterDBA


2 Answers

This should work:

streamWriter.BaseStream.Position;

This should contain the current position of the stream and if you're using it for appending only, this should contain the correct file size.

like image 113
Pieter van Ginkel Avatar answered Oct 06 '22 00:10

Pieter van Ginkel


FileSystemWatcher fsw=new FileSystemWatcher(filePath);
fsw.NotifyFilter=NotifyFilters.Size;
fsw.Filter="fileName";
fsw.Changed += new FileSystemEventHandler(YourHandler); 
fsw.EnableRaisingEvents = True;
like image 22
Bolu Avatar answered Oct 06 '22 00:10

Bolu