Solved

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

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

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!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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