473,410 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 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 1878
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
6
by: Jimbo | last post by:
After I read or write a file with streamreader and streamwriter and I close it with the Close method, does that automatically let go of the file so that any other process can modify it? In my...
6
by: Fabian Härle | last post by:
Hi, if I write to a tet file via a System.IO.TextWriter the changes to the file are not done before I close the file. Is there a possibility to always save the file with every change? The point...
6
by: Tim Barg | last post by:
I am using a CryptoStream to encrypt and then save an xml file. However, when reading it back in, I am getting a CryptographicException. I am hitting this because I have some large xml files ( >...
24
by: Kelly | last post by:
Hey all - I need a little more help. I don't quite know why my text file or form isn't closing. Short version - this program takes data entered into a textbox, user clicks Save button, Save...
7
by: TheRain | last post by:
Hey there, I am writing some test code, trying to hook standard IO from cmd.exe. What I really want to do is to open cmd.exe, keep it open, and have it process the DOS commands as I sent them...
10
by: John Kraft | last post by:
Hello all, I'm experiencing some, imo, strange behavior with the StreamReader object I am using in the code below. Summary is that I am downloading a file from a website and saving it to disk...
1
by: bioxx | last post by:
Hi, as the title suggests, I am having trouble making a thread close properly. I'll give a brief overview and then post relevent code. I have a server that needs to handle connections from multiple...
5
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
(a little more info) after flushing the line (instead of closing streamwirter) it takes some x time to flush the line so, when I read from the file I cannot find the last written line. if I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.