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

Problems with writing a text file (again)

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


P: n/a
"poldoj" <po********@gmail.com> schrieb:
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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2

P: n/a
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("123fabc")
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
Nov 21 '05 #3

P: n/a
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()

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

Thank all for help and patience :)
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.