I am confused about which is the better (or safer ) way to write files in
VB.Net.
Using sw As StreamWriter = New StreamWriter(fN ame)
For Each row As DataRow In ds.Tables(0).Ro ws
For i As Integer = 0 To (ds.Tables(0).C olumns.Count - 1)
col = row(i).ToString .Replace(","c, " "c) 'make sure no extra
commas
If (i < (ds.Tables(0).C olumns.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 5 7439
On Apr 30, 8:46 pm, "Harry Strybos" <harry_NOS...@f fapaysmart.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(fN ame)
For Each row As DataRow In ds.Tables(0).Ro ws
For i As Integer = 0 To (ds.Tables(0).C olumns.Count - 1)
col = row(i).ToString .Replace(","c, " "c) 'make sure no extra
commas
If (i < (ds.Tables(0).C olumns.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
Tom,
Nice showing expliciet the nicer code
Dim s As New StreamWriter ()
Cor
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
"Derek" <de***@nospam.c omschrieb:
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/>
"Tom Shelton" <to*********@co mcast.netwrote in message
news:11******** *************@y 5g2000hsa.googl egroups.com...
On Apr 30, 8:46 pm, "Harry Strybos" <harry_NOS...@f fapaysmart.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(fN ame) For Each row As DataRow In ds.Tables(0).Ro ws
For i As Integer = 0 To (ds.Tables(0).C olumns.Count - 1)
col = row(i).ToString .Replace(","c, " "c) 'make sure no extra commas
If (i < (ds.Tables(0).C olumns.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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);
|
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 I can
really tell if the file can be delete is to actually try to delete it and
catch an exception.. Trying to open a file with StreamReader in write mode
|
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 ?
Thanks and regards.
Marcel Hug
|
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 reason I need to do this is it has to be done on the web server.
Thanks,
|
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 written in file are not the sent bytes.
Copy and paste the following lines to observe my problem.
What can I do to resolve problem ?
Only System.Text.Encoding.ASCII write the same number of bytes, but not the
good bytes.
Someone can help me. Thanks by...
| |
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 that my csv file only contains the data from the last XML file
parsed...
I got the following code for creating of my StreamWriter:
public OrderFileCreator(string fileName)
|
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 it,
it throws an exception.
How to solave this problem? So second process can wait until first
process completes its processing on the file.
Thanks in advance
|
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 build
with these methods, will appear to encrypt and decrypt, but the
resulting decrypted file will be corrupted. I tried encrypting a .bmp
file and then decrypting, the resulting decrypted file under .NET 2.0
is garbage, the .NET 1.1 build works...
|
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 DOS Cmd when I
do a wild card dir, but they cannot be copied in the DOS Cmd box. I get "file
not found". Am I missing a step; not doing something? This is .NET 1.1.
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |