?
Solved

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

Posted on 2011-09-02
2
Medium Priority
?
308 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 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