How do I check if a file is empty in C#?
I need something like:
if (file is empty) { // do stuff } else { // do other stuff }
Now FileInfo. Length will show a size that is not zero. A solution for this is to check for Length < 6, based on the max size possible for byte order mark. If your file can contain single byte or a few bytes, then do the read on the file if Length < 6 and check for read size == 0.
Use FileInfo.Length:
if( new FileInfo( "file" ).Length == 0 ) { // empty }
Check the Exists property to find out, if the file exists at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With