473,372 Members | 1,606 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,372 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 1875
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.