Why streamReader didn't close handle?

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
Reply

Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Why streamReader didn't close handle?

 
0
  #1
Jan 11th, 2009
I write application which continously reads and rewrites files (this program converts web links in htm files to local links, because I want use this files offline). When I wrote procedure:

  1. // index.htm
  2. FileName = (string)kolekcjaPlikow.GetValue(0);
  3.  
  4. StreamReader myStreamReader = null;
  5. myStreamReader = File.OpenText(FileName);
  6.  
  7. bufor = myStreamReader.ReadToEnd();
  8. myReadBufor = bufor;
  9.  
  10. myStreamReader.Close(); (2)
  11.  
  12. StreamWriter myStreamWriter = null;
  13. myStreamWriter = File.CreateText(FileName); (1)

I get error:
The process cannot access the file 'index.htm' in line (1) because it is being used by another process.

I cannot understand this: why myStreamReader.Close() in line (2) will not release the handle? Is there any possibility release this handle?
Last edited by piotr_kast; Jan 11th, 2009 at 10:50 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,839
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 266
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Why streamReader didn't close handle?

 
0
  #2
Jan 11th, 2009
You don't have to do this StreamWriter myStreamWriter = null;
Try using this syntax :

using ( StreamWriter myStreamWriter = File.CreateText(FileName) )
{

}

That way you can forget Close, Dispose etc. C# will take care of it.

And btw. if you close an existing file, why do you want to create it afterwards?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Re: Why streamReader didn't close handle?

 
0
  #3
Jan 11th, 2009
Because all files which I wan't to rewrite belong to one big htm portal when one file depends from another. In this files we may find hyperlinks like eg:

<a href="http://dal.functionx.com/vcsharp/applications/algebra.htm">

which I must turn into

<a href="applications/algebra.htm">.

Besides this html pages contains graphics, scripts which I don't want move anywhere.

I assume that into bracket from

using ( StreamWriter myStreamWriter = File.CreateText(FileName) )
{

}

I put whole procedure which was after StreamWriter myStreamWriter = null;. Am I correct?

I assume that in this same way I may rewrite procedure StreamReader.
Last edited by piotr_kast; Jan 11th, 2009 at 3:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,839
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 266
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Why streamReader didn't close handle?

 
0
  #4
Jan 11th, 2009
I put whole procedure which was after StreamWriter myStreamWriter = null;. Am I correct?

I assume that in this same way I may rewrite procedure StreamReader.
You are correct.
But if you create new files use not the same filename you already used.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Re: Why streamReader didn't close handle?

 
0
  #5
Jan 11th, 2009
Don't worry. First I collect all filenames from saved htm portal pages into one big Array (for me as very beginner in C# programming is the eaziest way to collect all filenames) and later using "for" (I know number collected files) I read in sequence each filename, rearrange all necessary path in edited htm, rewrite it and get next on the list until I reach the end of the list.
Last edited by piotr_kast; Jan 11th, 2009 at 4:14 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC