Solved

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

Posted on 2011-09-02
2
292 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
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 35

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 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