473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

File.OpenWrite vs StreamWriter

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
May 1 '07 #1
5 7413
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

May 1 '07 #2
Tom,

Nice showing expliciet the nicer code
Dim s As New StreamWriter ()
Cor
May 1 '07 #3
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
May 1 '07 #4
"Derek" <de***@nospam.comschrieb:
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/>
May 1 '07 #5
"Tom Shelton" <to*********@comcast.netwrote in message
news:11*********************@y5g2000hsa.googlegrou ps.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
May 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomaz Pereira | last post by:
Hi; I use the method below to save the an image showed in a Picture Box using the SaveFileDialog. protected void SaveImage() { try { Stream stream = File.OpenWrite (this.CaminhoImagem);
2
by: David Krmpotic | last post by:
Hi All!! Could you please help me with three problems: 1. I would like to have a procedure that returned true if the file is writtable=deletable.. I tried everything but so far the only way...
4
by: Marcel Hug | last post by:
Hello NG ! I would like to test, if a file in my subtree is allready in use by an other application. Do anybody know how to get this information ? It is possible to get this information ? ...
3
by: Trint Smith | last post by:
Is there anything like this that will allow a url address instread of a drive letter?: Dim fw As StreamWriter fw = New StreamWriter("D:\file.txt", True) fw.WriteLine(ex) fw.Close() The...
5
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes...
4
by: Mads Westen | last post by:
Hi, I'm parsing some XML files, and converting them to CSV. I got x number of XML files in a directory, these files do I need to be converted and written to the same CSV file. My problem is...
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
5
by: =?Utf-8?B?Um9u?= | last post by:
I am writing out some files, using this syntax: FileStream fw = File.OpenWrite(Filename); fw.Write(bytearray,0,amount); fw.Close(); The files appear in Windows Explorer, they show up from the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.