Solved

c# - read text file then delete. Is there a better way than this?

Posted on 2013-07-01
5
587 Views
Last Modified: 2013-07-16
Hi,

I want to ReadAllLines from a text file, then delete the file. The file is created and written to by another application, so there is a chance of reading/deleting while the other application is trying to write.

This is what I have done. However Im sure there is a more professional way, any input appreciated.

private void ProcessFile(String fileName)
{
	string[] lines = File.ReadAllLines(fileName);

	while (true)
	{
		try
		{                
			System.IO.File.Exists(fileName);
			break;
		}
		catch (IOException)
		{
			Thread.Sleep(3000);
		} 
	} 
} 

Open in new window

0
Comment
Question by:mhdi
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
That doesn't delete a file.
System.IO.File.Exists(fileName);

it should be
System.IO.File.Delete(fileName);
0
 

Author Comment

by:mhdi
Comment Utility
Whoops, that was a silly mistake. I will fix that.

What do you think about the overall logic? Is there a better way than the try/catch sleep idea?
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 166 total points
Comment Utility
I suspect you intend this:

private void ProcessFile(String fileName)
{
      string[] lines = File.ReadAllLines(fileName);

      while (true)
      {
            try
            {                
                  if(System.IO.File.Exists(fileName))  System.IO.File.Delete(fileName);
                  break;
            }
            catch (IOException)
            {
                  Thread.Sleep(3000);
            }
      }
}

Otherwise it looks OK to attempt to delete and retry when fails.  
ps.  This must be in a separate thread else it will block the main thread of your app with the Sleep(3000) statement should the file not be deleted
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 167 total points
Comment Utility
best practice would be to have the writing app notify that writing is over, so the reader can read and process the file and then delete it.
if its not feasible, you should use other method then using while(true) cause u may ended up in infinite loop if the file can't be deleted.
so what i'd do is having a background task which tries to delete the file every some interval for a duration of predefined time.
so for example if the file cannot be deleted after 5 minutes, something is wrong, the writer might still hold a handle to the file or something else.
exception handling is also a great measure, considering that entering a try/catch block is relatively fast , the overhead is negligible and you have the option to catch the specific exceptions, like IOException when the file is in use.
for .net 4.0 u can use this extension:
public static class FileExtensions
    {
        public static void DeleteAsync(this FileInfo fi, TimeSpan duration)
        {
            if (fi.Exists)
            {
                var now = DateTime.Now;
                while (DateTime.Now < now.Add(duration))
                {
                    try
                    {
                        Task.Factory.StartNew(() => fi.Delete());
                    }
                    catch (IOException)
                    {
                        //The specified file is in use or there is an open handle on the file, so suppress this exception
                        //all other exceptions will be thrown
                    }
                }
            }
        }
    }

Open in new window


all other .net versions:
public static class FileExtensions
    {
        public static void DeleteAsync(this FileInfo fi, TimeSpan duration)
        {
            if (fi.Exists)
            {
                var now = DateTime.Now;
                while (DateTime.Now < now.Add(duration))
                {
                    try
                    {
                                     ThreadPool.QueueUserWorkItem(delegate
            {
                fi.Delete();
            }, null);
                    }
                    catch (IOException)
                    {
                        //The specified file is in use or there is an open handle on the file, so suppress this exception
                        //all other exceptions will be thrown
                    }
                }
            }
        }
    }

Open in new window



to use this code:
new FileInfo(@"c:\temp\1.csv").DeleteAsync(new TimeSpan(0, 1, 0));

Open in new window

0
 
LVL 13

Assisted Solution

by:jonnidip
jonnidip earned 167 total points
Comment Utility
Just a consideration.
If you want to READ the LATEST CONTENT of the file, I would rather try to "move" the file to another location/name and read it, while the other application writes a new file with the original name.

Just something like this:
String tempFileName = String.Empty;
File.Delete(tempFileName = Path.GetTempFileName());

while (true)
{
    try
    {
        File.Move(@"c:\temp\test.xml", tempFileName);
        break;
    }
    catch
    {
        System.Threading.Thread.Sleep(1000);
    }
}

String fileContent = File.ReadAllText(tempFileName);
File.Delete(tempFileName);

Open in new window

This will ensure you to have the latest content of the file, at the time it was moved.

Regards.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

12 Experts available now in Live!

Get 1:1 Help Now