By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,449 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

Leaking SafeFileHandles

P: n/a
Hello,

I've asked this before, but I never got a _good_ answer, so here's another
shot:

- Networks are unreliable, and servers cannot be guaranteed to have 100%
uptime.

After opening a file located on network drive (using FileStream and
exclusive locking), the network suddenly dies. The file is still locked by
the process, but cannot be reopened or closed.

// please enable your connection and open the file
FileStream fs1 = new FileStream(networkFile, FileMode.CreateNew,
FileAccess.Write, FileShare.Read);

// now, terminate your connection and do the following to fill the
FileStream buffers - this will ofcourse fail - BUT BUFFERS WILL BE FILLED
try {
byte[] ba = Encoding.Default.GetBytes("Hello World");
fs1.Write(ba, 0, ba.Length);
fs1.Flush();
} catch (Exception) { }

// now, enable your connection again (or don't), and try to close the file
// this will also fail since .Close() tries to flush buffers - resulting in
a file that can't be closed!!!
fs1.Close();
Does anyone got a solution for this specific problem? The file can't be
closed/disposed, the handle is still there, and it can't be reopened because
of the locking. Is there a way to _safely_ close the file - and release
possible handles?
Best Regards,
Fredrik Johansson
Apr 25 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.