Solved

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

Posted on 2011-09-02
2
283 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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now