CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2009
    Posts
    53

    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.

  2. #2
    Join Date
    Sep 2009
    Posts
    53

    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..

  3. #3
    Join Date
    Jul 2006
    Posts
    297

    Re: File.Delete File.Move Only Works in Debug

    Quote Originally Posted by demise View Post
    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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).

  5. #5
    Join Date
    Sep 2009
    Posts
    53

    Re: File.Delete File.Move Only Works in Debug

    Quote Originally Posted by Arjay View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured