DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   Why streamReader didn't close handle? (http://www.daniweb.com/forums/thread167708.html)

piotr_kast Jan 11th, 2009 10:49 am
Why streamReader didn't close handle?
 
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:

// index.htm
FileName = (string)kolekcjaPlikow.GetValue(0);

StreamReader myStreamReader = null;
myStreamReader = File.OpenText(FileName);

bufor = myStreamReader.ReadToEnd();
myReadBufor = bufor;

myStreamReader.Close(); (2)

StreamWriter myStreamWriter = null;
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?

ddanbe Jan 11th, 2009 2:37 pm
Re: Why streamReader didn't close handle?
 
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?

piotr_kast Jan 11th, 2009 3:57 pm
Re: Why streamReader didn't close handle?
 
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.

ddanbe Jan 11th, 2009 4:03 pm
Re: Why streamReader didn't close handle?
 
Quote:

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.

piotr_kast Jan 11th, 2009 4:13 pm
Re: Why streamReader didn't close handle?
 
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.


All times are GMT -4. The time now is 9:12 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC