471,315 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

Issue while closing a StreamWriter

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

Dec 13 '05 #1
3 1760
Hi

There different ways to do that. Here are two proposal - perhaps others
have any better idea.

1.) You can keep the stream-writer instance as long as you can not
close it correctly in the memory - which means you have to wait until
your network connection is coming back and then you close your
stream-writer in a correct way

2.) Your are copying your file temporarily into a local temp folder,
then you write your stuff and afterwards you replace the network-copy
on the network

Hope this helps
Roland

Dec 13 '05 #2

<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.
Dec 13 '05 #3

"Roland" <ro******************@gmx.net> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi

There different ways to do that. Here are two proposal - perhaps others
have any better idea.

1.) You can keep the stream-writer instance as long as you can not
close it correctly in the memory - which means you have to wait until
your network connection is coming back and then you close your
stream-writer in a correct way

You can't do this, when the session with the server recovers all open File
Handle becomes invalid at the server side. Also you should not try to re-use
the file as she will probably be corrupt after a network failure.
2.) Your are copying your file temporarily into a local temp folder,
then you write your stuff and afterwards you replace the network-copy
on the network

Hope this helps
Roland

Dec 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Fabian Härle | last post: by
24 posts views Thread by Kelly | last post: by
10 posts views Thread by John Kraft | last post: by
1 post views Thread by bioxx | last post: by
5 posts views Thread by =?Utf-8?B?cmF1bGF2aQ==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.