writing a text file in vb.net


P

poldoj

Hi all, I am trying to write the content of a variable to a text file. I am
currently switching from VB 6 code to VB.NET. Here is the code in my vb 6
applications (this code works)
------------------------
....
....
FileName$ = CommonDialog1.FileName
For i = 0 To List1.ListCount - 1
variable01 = variable01 & List1.List(i) & vbCrLf
Next
Const ForReading = 1, ForWriting = 2
Dim fso, f, ra
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(FileName$, ForWriting, True)
f.Write variable01
....
....
------------------------
This code just retrive the text from a list box and write a text file. I
have tried to convert to VB.Net without success, here's my attempt:
------------------------
....
....
Dim FileName As String

Dim hold_temp As String

If SaveFileDialog1.ShowDialog() = DialogResult.OK Then

FileName = SaveFileDialog1.FileName

Dim file As System.IO.FileStream

file = System.IO.File.Create(FileName)

file.Close()

Dim fw As StreamWriter

fw = New StreamWriter(FileName, False)

Dim MainArray() As String

MainArray = TextBox1.Lines

Dim i

For i = 0 To MainArray.GetUpperBound(0)

hold_temp += MainArray(i) & vbCrLf

Next

fw.Write(hold_temp)

fw.Close()

End If

....
....
------------------------
This code in vb.net create a text files that looks like at that in vb6 but
if I try to read it, it return me strange characters. I thinks I missed the
part with the "createobjetc" of the vb 6.



Thanks!
 
Ad

Advertisements

C

Cor Ligthert

Poldoj,

I think that it is posible that you make it yourself to difficult.

A sample how you can do it in my idea

\\\
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
dim FileName as String = SaveFileDialog1.FileName
'don't set a dim outside a routine when there is no need for it.
dim TempFileName as String
If File.Exist(FileName) then
System.IO.Path.GetTempFileName
File.Move(FileName,TempFileName)
End if
Dim sw As New StreamWriter(FileName)
Try
sw.write(TextBox1.Text)
sw.close
File.Delete(TempFileName)
Catch ex as exception
Messagebox.show(ex.ToString)
sw.close
File.Move(TempFileName,FileName)
End Try
End if
////

I wrote this completly in this messages, so watch typos or other errors.

I hope this helps a little bit?

Happy Newyear

Cor
 
J

Jim Edgar

I think that it is posible that you make it yourself to difficult.
A sample how you can do it in my idea

\\\
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
dim FileName as String = SaveFileDialog1.FileName
'don't set a dim outside a routine when there is no need for it.
dim TempFileName as String
If File.Exist(FileName) then
System.IO.Path.GetTempFileName
File.Move(FileName,TempFileName)
End if
Dim sw As New StreamWriter(FileName)
Try
sw.write(TextBox1.Text)
sw.close
File.Delete(TempFileName)
Catch ex as exception
Messagebox.show(ex.ToString)
sw.close
File.Move(TempFileName,FileName)
End Try
End if
////
Is there any performance difference between using File.Open/File.Close
instead of StreamWriter(). I'm transitioning from VB6 and the
book I'm reading from Microsoft Press says
there's no difference and it's only a matter of personal style.

Jim Edgar
 
Ad

Advertisements

C

Cor Ligthert

Jim,
Is there any performance difference between using File.Open/File.Close
instead of StreamWriter(). I'm transitioning from VB6 and the
book I'm reading from Microsoft Press says
there's no difference and it's only a matter of personal style.
I don't know, however in my opininon is the streamwriter general used for
the kind of operations you want to do now. I never heard something of
performance about this subject and surely would have seen that here when it
was. However the streamreader/writer is very easy to use.

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top