By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,965 Members | 2,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,965 IT Pros & Developers. It's quick & easy.

writing a text file in vb.net

P: n/a
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!
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I think your problem may be that you aren't specifying which encoding to
se ---
http://msdn.microsoft.com/library/de...ClassTopic.asp
However the text writer should do the job for you as well.
http://msdn.microsoft.com/library/de...classtopic.asp

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"poldoj" <po********@gmail.com> wrote in message
news:#t**************@tk2msftngp13.phx.gbl...
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!

Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
> 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
Nov 21 '05 #4

P: n/a
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
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.