File.Delete File.Move Only Works in Debug
This is an odd one.
With all the guts out I have this:
TextReader tr = new StreamReader("List.txt");
TextWriter tw = new StreamWriter("List2.txt");
//Guts
tw.Close();
tr.Close();
File.Delete("List.txt");
File.Move("List2.txt", "List.txt");
When I run this I get an error of:
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file 'List.txt' because it is being used by another process.
Yet.. When I debug and go through it step by step, it works perfectly.. Odd.. any clues?
Cheers.
Re: File.Delete File.Move Only Works in Debug
Oh Dear... there was a typo in one of the earlier tw, tr's. And this didnt kick up an error! Oops.
Shame there isnt a 'Remove Post' option :( Sorry. Still.... very odd how it would work while stepping through.. hmm..
Re: File.Delete File.Move Only Works in Debug
Quote:
Originally Posted by
demise
Oh Dear... there was a typo in one of the earlier tw, tr's. And this didnt kick up an error! Oops.
Shame there isnt a 'Remove Post' option :( Sorry. Still.... very odd how it would work while stepping through.. hmm..
It works stepping though because of the slower timing present while debugging.
Re: File.Delete File.Move Only Works in Debug
The problem is simply that the TextReader and TestWriter objects haven't been disposed.
Just wrap them in a couple of using blocks.
Code:
using( TextReader tr = new StreamReader("List.txt") )
{
using( TextWriter tw = new StreamWriter("List2.txt") )
{
//Guts
}
}
File.Delete("List.txt");
File.Move("List2.txt", "List.txt");
In a general sense, in my opinion, there aren't any 'timing' issues in Windows. Instead there are [lack of] synchronization issues.
In other words, if you view these sorts of problems as 'timing' related, you'll always have trouble because things happen differently in different environments. Instead view it as a synchronization issue and figure out what you need to do to synchronize the operations properly. In this case, it's just adding the using block so you know the file handles are released after leaving the using block scope (and before the Delete and Move operations are run).
Re: File.Delete File.Move Only Works in Debug
Quote:
Originally Posted by
Arjay
The problem is simply that the TextReader and TestWriter objects haven't been disposed.
Just wrap them in a couple of using blocks.
Code:
using( TextReader tr = new StreamReader("List.txt") )
{
using( TextWriter tw = new StreamWriter("List2.txt") )
{
//Guts
}
}
File.Delete("List.txt");
File.Move("List2.txt", "List.txt");
In a general sense, in my opinion, there aren't any 'timing' issues in Windows. Instead there are [lack of] synchronization issues.
In other words, if you view these sorts of problems as 'timing' related, you'll always have trouble because things happen differently in different environments. Instead view it as a synchronization issue and figure out what you need to do to synchronize the operations properly. In this case, it's just adding the using block so you know the file handles are released after leaving the using block scope (and before the Delete and Move operations are run).
Great bit of info there, Thanks.