?
Solved

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

Posted on 2013-07-01
5
Medium Priority
?
677 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
[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
5 Comments
 
LVL 44

Expert Comment

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

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

Author Comment

by:mhdi
ID: 39292287
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 664 total points
ID: 39292296
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 668 total points
ID: 39292325
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 668 total points
ID: 39292694
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

800 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