Reputation: 34740
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:
currFileInfo = new FileInfo(fileNameStr);
curFileInfo.Refresh(); fileSize = curFileInfo.Length;
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?
Upvotes: 5
Views: 479
Reputation: 8786
FileSystemWatcher fsw=new FileSystemWatcher(filePath);
fsw.NotifyFilter=NotifyFilters.Size;
fsw.Filter="fileName";
fsw.Changed += new FileSystemEventHandler(YourHandler);
fsw.EnableRaisingEvents = True;
Upvotes: 1
Reputation: 29632
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.
Upvotes: 6