Releasing file locks

Discussion in 'ASP .Net' started by David Veeneman, Feb 14, 2007.

  1. I have a maddening problem that I hope someone can help with. I have some
    code that writes temporary documents to the data folder of my web site. On
    one document, I keep getting an error "The process cannot access the file
    'myFile.txt' because it is being used by another process."

    Simple failure to wrap a stream writer in a try-catch-finally block, right?
    Not so--here's the code in question:

    ***********************************************************************************

    string filePath = String.Format(@"{0}\Documents\myFile.txt", dataPath);
    string msg = String.Empty;
    try
    {
    // Create StreamWriter
    streamWriter = new StreamWriter(filePath, false);

    // Write file
    streamWriter.WriteLine(SomeText);
    }
    catch (Exception e)
    {
    // Log error message
    msg = e.Message;
    }
    finally
    {
    // Close and dispose
    if (streamWriter != null)
    {
    streamWriter.Close();
    streamWriter.Dispose();

    // Log if the file stream was closed
    msg = "File stream closed and disposed";
    }
    }

    // Log result
    ActivityLog.WriteEntry(msg, dataPath);

    *********************************************************************

    Here's where it gets really strange: We uncovered the problem in testing. We
    can replicate it by forcing an exception in another part of the code for
    this page. In other words, the original error is not related to the
    temporary file in any way, and we have verified that the Close() method for
    this file stream is being called long before the original exception is ever
    raised. But the next time the page is loaded, we get the "cannot access"
    error.

    In other words, even though the file stream is properly closed when the file
    is created, on the next page load, it is locked, causing a "cannot access"
    exception.

    Has anyone come across anything like this? I am totally baffled. Any
    suggestions as to how to proceed would be appreciated.

    David Veeneman
    Foresight Systems
     
    David Veeneman, Feb 14, 2007
    #1
    1. Advertising

  2. The short answer: Look for locks in other parts of the program, which is
    where I found my solution.
     
    David Veeneman, Feb 14, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    0
    Views:
    487
  2. David Veeneman

    Releasing lock on a text file?

    David Veeneman, Feb 9, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    361
  3. David Veeneman

    Releasing file locks

    David Veeneman, Feb 14, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    365
    David Veeneman
    Feb 14, 2007
  4. David Veeneman

    Releasing file locks on email attachments?

    David Veeneman, Feb 15, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    500
  5. DG

    Web service is not releasing SQL05 locks

    DG, Nov 20, 2007, in forum: ASP .Net Web Services
    Replies:
    5
    Views:
    340
Loading...

Share This Page