Re: StreamWriter Problem



On Dec 8, 1:45 am, Tom Shelton <tom_shel...@xxxxxxxxxxx> wrote:
On Dec 7, 4:23 pm, kimiraikkonen <kimiraikkone...@xxxxxxxxx> wrote:

Edit: However, i added a catch ex as exception handler and received
the exception as "the file is being used by another process" although
nothing is open except my program.

Is it a streamwriter bug? Because nothing is using a text file while
i'm trying to save through my project.

no, it's not a bug in the streamwriter - it's a bug in your code. and
if your code for loading the file is similar to the saving code, then
i can see why. you are never closing the resource. you need to call
close on the streamwriter (and streamreader) when you are done with
them....

using sr as new streamreader(myfile)
' do stuff
end using

using the using block will ensure that the streamreader is properly
cleand up when the block ends. Basically, it will compile like you
had done something like:

dim sr as streamreader = nothing
try
sr = new streamreader(myfile)
' do stuff
finally
if not isnothing(sr) then
sr.dispose()
end finally

you need to do the same with your streamreader.

--
Tom Shelton

I was using this for streamwriter also of streamreader before i posted
with no help:
Try
......
Catch

Finally
If Not myStreamWriter Is Nothing Then
myStreamWriter.Close()
End If
.



Relevant Pages

  • Re: Bug in StreamReader?
    ... there is nothing in the documentations saying that it opens a file ... as read only, so the name StreamReader may be misleading, and in fact, the ... > Yes this would certainly solve the problem but it is really a bug in the ...
    (microsoft.public.dotnet.general)
  • Re: StreamWriter Problem
    ... the exception as "the file is being used by another process" although ... it's not a bug in the streamwriter - it's a bug in your code. ... using the using block will ensure that the streamreader is properly ... Tom Shelton ...
    (microsoft.public.dotnet.languages.vb)
  • Re: StreamWriter Problem
    ... the exception as "the file is being used by another process" although ... it's not a bug in the streamwriter - it's a bug in your code. ... using the using block will ensure that the streamreader is properly ... Tom Shelton ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Wite and Read to same file ERROR ?
    ... Thats because StreamReader already opens the File as Read&Write exclusive ... (although is can only read from the file, I posted this as bug to MS but ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The process cannot access the file (in Windows2003 only)
    ... Dim sr As StreamReader ... catch ex as exception ... recently moved it onto a new Windows2003 Server Service Pack 2 machine. ... Dim sr As StreamReader = File.OpenText ...
    (microsoft.public.dotnet.languages.vb)

Loading