Home All Groups Group Topic Archive Search About

File.OpenWrite vs StreamWriter

Author
1 May 2007 2:46 AM
Harry Strybos
I am confused about which is the better (or safer ) way to write files in
VB.Net.

Using sw As StreamWriter = New StreamWriter(fName)
    For Each row As DataRow In ds.Tables(0).Rows

        For i As Integer = 0 To (ds.Tables(0).Columns.Count - 1)

            col = row(i).ToString.Replace(","c, " "c) 'make sure no extra
commas

            If (i < (ds.Tables(0).Columns.Count - 1)) Then

                    sw.Write(col & ",")

            Else

                    sw.Write(col)

            End If

        Next

        sw.WriteLine()

    Next

    sw.Close()

End Using

seems very simple, but I am told this method leaves a file open if an error
occurs. Is the File.OpenWrite a safer method?

Thank you

Author
1 May 2007 4:24 AM
Tom Shelton
Show quote Hide quote
On Apr 30, 8:46 pm, "Harry Strybos" <harry_NOS***@ffapaysmart.com.au>
wrote:
> I am confused about which is the better (or safer ) way to write files in
> VB.Net.
>
> Using sw As StreamWriter = New StreamWriter(fName)
>     For Each row As DataRow In ds.Tables(0).Rows
>
>         For i As Integer = 0 To (ds.Tables(0).Columns.Count - 1)
>
>             col = row(i).ToString.Replace(","c, " "c) 'make sure no extra
> commas
>
>             If (i < (ds.Tables(0).Columns.Count - 1)) Then
>
>                     sw.Write(col & ",")
>
>             Else
>
>                     sw.Write(col)
>
>             End If
>
>         Next
>
>         sw.WriteLine()
>
>     Next
>
>     sw.Close()
>
> End Using
>
> seems very simple, but I am told this method leaves a file open if an error


> occurs. Is the File.OpenWrite a safer method?

You have been miss informed - when the resource is declared with a
using block, it's dispose method is automatically called as soon as
you exit the block - even if that is via an error condition.  In fact,
your sw.Close() is redundant - since the stream will be closed
automatically on the End Using.  You can think of Using as being
essentially the same as:

Dim s As New StreamWriter ()
Try
     ' a bunch of stuff
Finally
   s.Close()
End Try

--
Tom Shelton
Author
1 May 2007 5:07 AM
Cor Ligthert [MVP]
Tom,

Nice showing expliciet the nicer code

> Dim s As New StreamWriter ()

Cor
Author
1 May 2007 9:23 AM
Derek
Just curious but does the "Using" re-throw the exception afterwards?  It
wouldn't be very nice to have my using statements hiding exceptions from the
rest of my code.

i.e.

Try

Catch eX as Exception

    Throw

Finally

    ' Dispose

End Try
Author
1 May 2007 1:44 PM
Herfried K. Wagner [MVP]
"Derek" <derek@nospam.com> schrieb:
> Just curious but does the "Using" re-throw the exception afterwards?

Yes.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
1 May 2007 8:36 PM
Harry Strybos
Show quote Hide quote
"Tom Shelton" <tom_shel***@comcast.net> wrote in message
news:1177993473.172596.97010@y5g2000hsa.googlegroups.com...
> On Apr 30, 8:46 pm, "Harry Strybos" <harry_NOS***@ffapaysmart.com.au>
> wrote:
>> I am confused about which is the better (or safer ) way to write files in
>> VB.Net.
>>
>> Using sw As StreamWriter = New StreamWriter(fName)
>>     For Each row As DataRow In ds.Tables(0).Rows
>>
>>         For i As Integer = 0 To (ds.Tables(0).Columns.Count - 1)
>>
>>             col = row(i).ToString.Replace(","c, " "c) 'make sure no extra
>> commas
>>
>>             If (i < (ds.Tables(0).Columns.Count - 1)) Then
>>
>>                     sw.Write(col & ",")
>>
>>             Else
>>
>>                     sw.Write(col)
>>
>>             End If
>>
>>         Next
>>
>>         sw.WriteLine()
>>
>>     Next
>>
>>     sw.Close()
>>
>> End Using
>>
>> seems very simple, but I am told this method leaves a file open if an
>> error
>
>
>> occurs. Is the File.OpenWrite a safer method?
>
> You have been miss informed - when the resource is declared with a
> using block, it's dispose method is automatically called as soon as
> you exit the block - even if that is via an error condition.  In fact,
> your sw.Close() is redundant - since the stream will be closed
> automatically on the End Using.  You can think of Using as being
> essentially the same as:
>
> Dim s As New StreamWriter ()
> Try
>     ' a bunch of stuff
> Finally
>   s.Close()
> End Try
>
> --
> Tom Shelton
>

Thanks, guys