StreamReader and "using" statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackjack2150
    New Member
    • Feb 2007
    • 79

    StreamReader and "using" statement

    Hi.
    In my application I write errors which may occur to text file like this:

    using(StreamRea der sw = new SteamReader(err ors.txt, true))
    {
    sw.WriteLine("E rror message here");
    }

    Sometimes, the program says that it cannot open "errors.txt " because it is being used by another process. The only process that might be using it is my application. So basically it's a file lock problem.

    Question: If I use the StreamReader inside a "using" statement, is it necessary to call "Close()" it after I call "WriteLine( )"?

    Or if you know of a better approach, please suggest.

    Thanks.
  • mikeyeli
    New Member
    • Oct 2007
    • 63

    #2
    Sorry double posted..
    Last edited by mikeyeli; Feb 19 '08, 06:09 PM. Reason: double post

    Comment

    • mikeyeli
      New Member
      • Oct 2007
      • 63

      #3
      Originally posted by blackjack2150
      Hi.
      In my application I write errors which may occur to text file like this:

      using(StreamRea der sw = new SteamReader(err ors.txt, true))
      {
      sw.WriteLine("E rror message here");
      }

      Sometimes, the program says that it cannot open "errors.txt " because it is being used by another process. The only process that might be using it is my application. So basically it's a file lock problem.

      Question: If I use the StreamReader inside a "using" statement, is it necessary to call "Close()" it after I call "WriteLine( )"?

      Or if you know of a better approach, please suggest.

      Thanks.

      this statement:
      Code:
      StreamReader sw = new SteamReader(errors.txt, true)
      assigns an open stream to sw.

      so yes you must close it.

      why do you need to use a "using" statement?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by mikeyeli
        this statement:
        Code:
        StreamReader sw = new SteamReader(errors.txt, true)
        assigns an open stream to sw.

        so yes you must close it.

        why do you need to use a "using" statement?
        The using actually closes that stream automatically.

        Comment

        • mikeyeli
          New Member
          • Oct 2007
          • 63

          #5
          Originally posted by r035198x
          The using actually closes that stream automatically.
          oh i didnt know that, thanks for the correction r035198x.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Under what conditions is this statement being hit?
            Is it being done in or called from an event handler?
            Is it possible it could still be trying to write the data from a previous call when another one comes in and tries to write, so it errors?

            Comment

            Working...