views:

356

answers:

4

I have this code that saves a pdf file.

FileStream fs = new FileStream(SaveLocation, FileMode.Create);
fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
fs.Flush();
fs.Close();

It works fine. However sometimes it does not release the lock right away and that causes file locking exceptions with functions run after this one run.

Is there a ideal way to release the file lock right after the fs.Close()

+10  A: 

Here's the ideal:

using (var fs = new FileStream(SaveLocation, FileMode.Create))
{
    fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
}

which is roughly equivalent to:

FileStream fs =  null;
try
{
    fs = new FileStream(SaveLocation, FileMode.Create);
    fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
}
finally
{
    if (fs != null)
    {
        ((IDisposable)fs).Dispose();
    }
}

the using being more readable.


UPDATE:

@aron, now that I'm thinking

File.WriteAllBytes(SaveLocation, result.DocumentBytes);

looks even prettier to the eye than the ideal :-)

Darin Dimitrov
This is a great example of how to use the `using` statement, but since it's ultimately equivalent to the OP's code, it does nothing to address the question that was asked.
Jeffrey L Whitledge
@Jeffrey, I know that I drink lots of beer but I really don't see how this is equivalent to the OPs code.
Darin Dimitrov
Dispose calls Close. Close calls Flush. The end result is the same. Your change is benificial to avoid accidently leaving the file open in the case of exceptions or early returns, but it doesn't cause the file lock to be removed any quicker.
Jeffrey L Whitledge
equivalent in basic functionality. However this code is radically different in it's handling of garbage collection.. Which is the OP's problem. +1
Chris Lively
@Chris Lively - Close() will release the unmanaged resources. The garbage collection will be exactly the same in both cases. This problem has nothing to do with garbage collection.
Jeffrey L Whitledge
@Chris Lively - I just verified my suspicion that calling Close() will also call GC.SurpressFinalize(). The garbage collection is identical between Close() and Dispose().
Jeffrey L Whitledge
+1 A using() statement is not the solution here, we have seen this issue even with every write being wrapped in using(). See my answer for more comments.
Hightechrider
Jeffrey is right. `using` doesn't answer the real problem, unfortunately.
Nayan
+3  A: 

I can't imagine why the lock would be maintained after the file is closed. But you should consider wrapping this in a using statment to ensure that the file is closed even if an exception is raised

using (FileStream fs = new FileStream(SaveLocation, FileMode.Create))
{
  fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);  
}
Chris Taylor
+3  A: 

If the functions that run after this one are part of the same application, then a better approach might be to open the file for read/write at the beginning of the entire process, and then pass the file to each function without closing it until the end of the process. Then it will be unnecessary for the application to block waiting for the IO operation to complete.

Jeffrey L Whitledge
+2  A: 

We have seen this same issue in production with a using() statement wrapping it.

One of the top culprits here is anti-virus software which can sneak in after the file is closed, grab it to check it doesn't contain a virus before releasing it.

But even with all anti-virus software out of the mix, in very high load systems with files stored on network shares we still saw the problem occasionally. A, cough, short Thread.Sleep(), cough, after the close seemed to cure it. If someone has a better solution I'd love to hear it!

Hightechrider