Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is file empty check

Tags:

c#

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 } 
like image 579
Arcadian Avatar asked Jun 09 '10 16:06

Arcadian


People also ask

How check if file is empty C#?

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.


1 Answers

Use FileInfo.Length:

if( new FileInfo( "file" ).Length == 0 ) {   // empty } 

Check the Exists property to find out, if the file exists at all.

like image 188
tanascius Avatar answered Sep 23 '22 01:09

tanascius