Write Text to File, Write Binary To File (VB.NET)

A quick note.

There is not such thing as writing text to file. Eventually, everything will be in bytes.

Thus, it is more proper to say that 'Save Text To File' and 'Save Picture To File'.

First of all, it is important to understand two things.
  1. The difference between string and byte (binary)
  2. The difference between ANSI and Unicode.
Refer to 'ANSI vs Unicode'

A Few Important Things:

1. If you want to create new file, you have to use SaveFileDialog , and open existing file, use OpenFileDialog. The latter will not allow you to create new file.

2. Since you can use FileStream to write data to the files, why do you still need BinaryWriter and StreamWriter (TextWriter ) ?
  • BinaryWriter and StreamWriter(TextWriter) are extensions to FileStream. Using FileStream, you have to convert everything to bytes, using StreamWriter , no need to convert string to bytes.
  • BinaryWriter and StreamWriter(TextWriter) has more overloaded methods for write() than FileStream? itself. Using streamwriter, you have a lot of overloaded methods, such as for writing data to newline.If using filestream, you have to manually add vbcrlf.
  • FileStream supports asynch process via BeginWrite and BeginRead methods. For BinaryWriter and StreamWriter, you have to create custom asynch mechanism.

To understand this further, download the sample code in VB.NET (Visual Studio SP 1)