473,549 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if System.IO.Strea mWriter write throws an exception, is there anyway to close the System.IO.Strea mWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another proces

if System.IO.Strea mWriter write throws an exception, is there anyway to
close the System.IO.Strea mWriter object? it seems to stay open when this
happens then future attempts to write to that same path fail because it says
its in use by another process.
Nov 12 '05 #1
2 2923
Daniel wrote:
if System.IO.Strea mWriter write throws an exception, is there anyway to
close the System.IO.Strea mWriter object? it seems to stay open when this
happens then future attempts to write to that same path fail because it says
its in use by another process.


The finally-block has been invented for this (untested):

StreamWriter w;
try {
w = new StreamWriter(nu ll);
// 1. an exception is thrown
}
catch {
Debug.WriteLine ("!");
// 2. you handle it
}
finally {
/* 3. you clean up behind you, no matter if the exception was thrown
or not! */
if( w != null ){
w.Close();
w = null;
}
}
--
Pascal Schmitt
Nov 12 '05 #2
If my System.IO.Strea mWriter Write method throws "The specified network name
is no longer available." and I try to Dispose or Close it in the finaly
clause the close or dispose method just throws "The specified network name
is no longer available." again. how to clean this up? after this the stream
writer is stuck connected until i restart my process.

"Pascal Schmitt" <ne*******@cebr a.nu> wrote in message
news:en******** ********@TK2MSF TNGP12.phx.gbl. ..
Daniel wrote:
if System.IO.Strea mWriter write throws an exception, is there anyway to
close the System.IO.Strea mWriter object? it seems to stay open when this
happens then future attempts to write to that same path fail because it says its in use by another process.


The finally-block has been invented for this (untested):

StreamWriter w;
try {
w = new StreamWriter(nu ll);
// 1. an exception is thrown
}
catch {
Debug.WriteLine ("!");
// 2. you handle it
}
finally {
/* 3. you clean up behind you, no matter if the exception was thrown
or not! */
if( w != null ){
w.Close();
w = null;
}
}
--
Pascal Schmitt

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
9595
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
3
419
by: Daniel | last post by:
if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another process.
4
4213
by: Bhavya Shah | last post by:
Hello, I am facing a very strange problem in my application. I have a form on which I select a path. I open the FolderBrowserDialog for path selection. Once the path is selected I press a button "Search" for searching documents on that path. This search process runs in a separate thread so that I can search for multiple paths simultaneously....
9
4575
by: ShadowOfTheBeast | last post by:
Hi, I have got a major headache understanding streamReader and streamWriter relationship. I know how to use the streamreader and streamwriter independently. but how do you write out using the streamwriter, what you have read into a streamReader? and also can someone explain how they work in simple terms -- The Matrix Insurrection
1
5503
by: Daniel | last post by:
If my System.IO.StreamWriter Write method throws "The specified network name is no longer available." and I try to Dispose or Close it in the finaly clause the close or dispose method just throws "The specified network name is no longer available." again. how to clean this up? after this the stream writer is stuck connected until i restart my...
6
13262
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileID.ToString() + ".pdf");
6
10204
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error message: "The process cannot access the file "whatever" because it is being used by another process." I've even tried opening another file using...
7
11081
by: Mark Rae | last post by:
Hi, Has anyone successfully used the FTP stuff in the System.Net namespace against a VMS FTP server? I'm trying to do this at the moment and can't even get a directory listing, although there are no problems using the Chilkat FTP component, or connecting and browsing manually with a variety of FTP clients. The code I'm using is:
3
3983
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user selects? thanks...
14
2554
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
0
7957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.