|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
File.OpenWrite vs StreamWriterVB.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
Show quote
Hide quote
On Apr 30, 8:46 pm, "Harry Strybos" <harry_NOS***@ffapaysmart.com.au> You have been miss informed - when the resource is declared with awrote: > 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? 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 Tom,
Nice showing expliciet the nicer code > Dim s As New StreamWriter () CorJust 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 "Derek" <derek@nospam.com> schrieb: Yes.> Just curious but does the "Using" re-throw the exception afterwards? -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Show quote
Hide quote
"Tom Shelton" <tom_shel***@comcast.net> wrote in message Thanks, guysnews: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 >
Help with delegate callback error
Strange Date Problem Problem with: Use the following method to smooth edges of screen fonts: if ClearType is selected Question about sending email via Visual Basic 2005 on a ASP.NET pa Can someone help me with this menu? debug mode faster than bin\exe ? Forms Perform transaction on 2 databases on 2 different servers. Export procedure entry point in a dll Window that shows up and disappears no matter what |
|||||||||||||||||||||||