Problems with writing a text file (again)

P

poldoj

First I would thank you all for the replies at my previous post. Say sorry
for my poor english also. I have found where my problem came from. I am
writing a string to a text box and write the content of that textbox to a
file. All is just fine until I try to pass this character to the textbox:

exemple:

------

TextBox1.AppendText(Chr(167) )

-------

or

-------

TextBox1.AppendText("§")

-------

that is the same thing. Then I have this code in the click button event (the
save button):

-------

Try

Dim file_name As String

SaveFileDialog1.Filter = "text files (*.txt)|*.txt|all files (*.*)|*.*"

If SaveFileDialog1.ShowDialog() = DialogResult.OK Then

file_name = SaveFileDialog1.FileName

Dim file As System.IO.FileStream

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

file.Close()

Dim fw As New StreamWriter(file_name)

fw.Write(TextBox1.Text)

fw.Close()

End If

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

------------

My text file is generated without problems, if I try to open it with notepad
just display my character "§"

But if I open this file with wordpad it show "§". When I try to read this
files from another applications I get this unwanted  character. I don't
know how to pass this "§" in the right way for display only the §.

Thanks.
 
H

Herfried K. Wagner [MVP]

poldoj said:
My text file is generated without problems, if I try to open it with
notepad just display my character "§"

But if I open this file with wordpad it show "§". When I try to read this
files from another applications I get this unwanted  character. I don't
know how to pass this "§" in the right way for display only the §.

You may want to use a different encoding when writing the file. At least
one overload of the 'StreamWriter' takes a 'System.Text.Encoding'.
 
C

Cor Ligthert

Poldoj,

I have had this problem as well. It was when I had set for some testing my
computer to another country which was using another codeset than I thought I
was using, not any encoding did help me than.

After setting it back all problems where gone.

Maybe you can check that if you did the same.

And then when that is not enough you can look which codeset you are using
and than something as

\\\
Public Shared Sub main()
If File.Exists("C:\Test2\Text.txt") Then _
File.Delete("C:\Test2\StringText.txt")
Dim stw As New StreamWriter("C:\Test2\StringText.txt")
stw.WriteLine("123âäfabc")
stw.Close()
Dim Str As New StreamReader("C:\Test2\StringText.txt")
Dim arrInput As Byte() = _
System.Text.Encoding.GetEncoding(437).GetBytes(Str.ReadToEnd)
Str.Close()
End Sub
////

Where 437 is one of the codetables that is used on my computer (You can as
well try 1252 (latin))

Cor
 
P

poldoj

I finally found the solution:
----------------------
Dim SwBuffer = New StreamWriter(file_name, True,
System.Text.Encoding.Default, 512)

SwBuffer.Write(TextBox1.Text)

SwBuffer.Flush()

SwBuffer.Close()
 

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