Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

VB.net - File Error

  • Please log in to reply



    New Member

  • Member
  • Pip
  • 3 posts

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


Attached Files

  • Attached File  test.txt   32bytes   292 downloads

  • 0




    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
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

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP