<mo*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Iam using streamwriter class of .net to write in a file. The location
of
file is a network drive. Now if for some reason the network is not
there
and I execute
sw.close I get a exception ( that's ok).
But later when network comes and I try to delete this file, it says
that the
file is being used by a process. How to release the file handle in such
a
case.
Thanks,
Mohit
The reason you can't delete the file is because the file handle is still in
use at the client side. Only possibility to recover from this, is to pass
the FileStream.Handle to the Win32 API CloseHandle when such exception
occurs, doing so will release the OS file handle so you can re-open or
(better) delete the file.
[ DllImport("Kernel32") ]
public static extern bool CloseHandle(IntPtr handle);
....
FileStream fs;
try {
...
}
catch(IOException)
{
// If resource no longer available, or unable to write to.....
if(...)
CloseHandle(fs.Handle);
}
Willy.