Solved

How to check if a type txt file is empty using C# with Visual Studio 2005?

Posted on 2011-09-02
2
300 Views
Last Modified: 2013-12-17
I am developing a C# console application using Visual Studio 2005.

I use the following statement in my program:
if (File.Exists(errorFilePath))

Is there a way to open this txt type file and see if this file is empty?
I want to delete this file, if it is an empty file.
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Expert Comment

by:Mez4343
ID: 36476035
Check the File.Lenth
if (File.Length==0)
                                Console.Writeline("File is empty.");
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 36476098
There is no File.Length method in the File class, but use FileInfo:
if (File.Exists(errorFilePath))
{
  if (new FileInfo(errorFilePath).Length == 0)
  {
          Console.WriteLine("{0} is empty", errorFilePath);
          File.Delete(errorFilePath);
   }
}  
Check:
http://www.dotnetperls.com/fileinfo-length
http://stackoverflow.com/questions/3007711/is-file-empty-check
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question