Writing/Reading Binary Files

johnadonaldson

Well-known member
Joined
Nov 9, 2005
Messages
48
Programming Experience
10+
Can someone give me a reference to a article or code that will allow me to Read/Write Binary Files. I have code for reading INI and XML files, but from what I understand you can not Write INI or XML files from VB.NET. So need to use Binary files for reading/configuration files.
 
Of course you can read and also write out to binary file ... say this is an example how to write out to binary file:

VB.NET:
Dim str As String = "Text text" 
Dim num As Integer = 123 
Dim binWriter As New BinaryWriter(File.OpenWrite("c:\myBin.dat")) 
binWriter.Write(str) 
binWriter.Write(num) 
binWriter.Close()

Regards ;)

It is also valid for XML ... Moreover, you'll find that .NET has almost perfect support for XML no matter writte, read or anything else ... actually in .NET you can say that XML are data and that data are XML ... that's the same :)
reading/writing from within XML is more easier than you expect ... ask for an example if you need.
 
kulrom...
Thank you for response. Yes, I would like to see a code sample for reading/writing XML files. I thought you could only READ XML under VB.NET. If I can also write them, that would be perfect for what I need.
 
Well, we have considered this subject many times before but i am a bit lazy to search around the threats ...
Get this example and you'll see how simple writing to XML file really is:
http://www.freevbcode.com/ShowCode.asp?ID=2789

Hmm ... ok let's improvise here as well (for a while :D)
To write to XML files you need XmlWriter class. The XmlWriter class contains the functionality to write to XML documents. It is an abstract base class used through XmlTextWriter and XmlNodeWriter classes. Well, simply import the System.Xml namespace and instantiate new XmlWriter creating a new XML file for instance:

VB.NET:
[COLOR=darkgreen]'this code will create a new XML file[/COLOR]
[COLOR=blue]Dim [/COLOR]myWriter [COLOR=blue]As[/COLOR] XmlTextWriter = [COLOR=blue]New[/COLOR] XmlTextWriter(" C:\myXML.xml", [COLOR=blue]Nothing[/COLOR])


After creating an instance, first thing you call is WriterStartDocument. When you’re done writing, you call WriteEndDocument and TextWriter’s Close method. And that's it ... voila :)

VB.NET:
myWriter.WriteStartDocument()
{...} [COLOR=green]'here y r writing the elements[/COLOR]
myWriter.WriteEndDocument()
myWriter.Close()
 
Last edited:
Back
Top