Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

VB.net - File Error


  • Please log in to reply

#1
rickbeach08

rickbeach08

    New Member

  • Member
  • Pip
  • 3 posts
Hi,

I have a problem with vb.net - writing a text file:

Very simple example -

My.Computer.FileSystem.WriteAllText("C:\tmp\test.txt", _
"This is new text to be added.",False)

Problem is it writes out a garbage character in first line so apps cannot use file

Attached is the file...not sure what I could be doing wrong...any help would be greatly appreciated

rick

Attached Files

  • Attached File  test.txt   32bytes   349 downloads

  • 0

Advertisements


#2
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
rickbeach08,
Your test.txt works fine on my PC.
Maybe the problem on your PC is that your System's default text encoding might be different from the .NET Framework's default text-encoding (By default the .NET Framework uses UTF8).
If you notice the My.Computer.FileSystem.WriteAllText() method has another overload which has 4 parameters. The 4th parameter specifies the text-encoding to be used. If you use the overload with 3 parameters, by default .NET uses UTF8, which is different from that of your system's, your getting the garbage values.
So all you have to do is specify the 4th parameter to that of your system, in the 2nd overload. If you don't know your system's text encoding, just use System.Text.Encoding.Default as your 4th parameter.
So, your method call will be:
My.Computer.FileSystem.WriteAllText("C:\tmp\test.txt", _
"This is new text to be added.",False,System.Text.Encoding.Default)

Edited by darth_ash, 22 March 2006 - 11:57 PM.

  • 0






Similar Topics

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP