locked
File class and size RRS feed

  • Question

  • Can I get the size of a file or check if it is empty using the File class or do I have to use FileInfo.

    Thanks
    Monday, April 13, 2009 5:20 PM

Answers

  • You can use File.ReadAllBytes and compare the result to zero to determine if the file is a zero-length file, but it would be better to use FileInfo.


    David Morton - http://blog.davemorton.net/
    • Marked as answer by useruser1 Monday, April 13, 2009 5:29 PM
    Monday, April 13, 2009 5:25 PM

All replies

  • You can use File.ReadAllBytes and compare the result to zero to determine if the file is a zero-length file, but it would be better to use FileInfo.


    David Morton - http://blog.davemorton.net/
    • Marked as answer by useruser1 Monday, April 13, 2009 5:29 PM
    Monday, April 13, 2009 5:25 PM
  • Great Thanks!
    Monday, April 13, 2009 5:29 PM
  • FileInfo is indeed better suited for the problem, its abit of a waste to read the complete file just to figure out its size, it'll probably work for smaller files but when you hit files of serveral (hundred) megs you're paying a big performance penalty.

    System.IO.FileInfo fileInfo = new System.IO.FileInfo(@"c:\blah.txt");
    Console.WriteLine("Size = {0}",fileInfo.Length);

    Monday, April 13, 2009 5:40 PM
  • If you can stomach the words Visual Basic, you can use the FileLen function.
    Monday, April 13, 2009 7:24 PM