none
Writing to TextFile -- Getting Exception RRS feed

  • Question

  • I am trying to write to a file in my application, First Time it writes fine, If I write to the file second time, I get an error.

     

     

    My Code -

     

     

    // l_FilePath is the path of my TextFile

     

    using (System.IO.StreamWriter l_outFile = new StreamWriter(l_FilePath, false))

    {

    l_outFile.Write("My Message to be write in the file");

    l_outFile.Close();

    }

     

     

    I get exception

     

    The process cannot access the file '' because it is being used by another process.\r\n at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\r\n

     

     

     

    iF I close the application and retry it the firsttime  it writes but the second time it gives me an error.

     

     

    Any Help/Clue is appreciated..

     

     

    Thanks,

    Mithun

    Saturday, May 24, 2008 7:38 PM

All replies

  • I expect that your application has the file open from the first operation. I would either delete the target file each time or choose an "append" option...

     

    Sunday, May 25, 2008 10:56 PM
    Moderator
  • YES, I am doing the same(i.e trying delete the file), but still I get the same error.

     

     

    Any work arounds for this problem.

     

     

     

    Tuesday, May 27, 2008 3:44 PM
  • Okay, then you need to ensure that you close the stream each time you open it. Don't forget to include a close in the Try/Catch/Finally block.

     

    Tuesday, May 27, 2008 8:26 PM
    Moderator