Venkat
In addition to Herfried's excellent help.
I see that the two file sizes are not the same.
Can you tell me why?
BinaryWriter has overloaded Write methods, you asked to write a String.
BinaryWriter.Write(String) writes the encoded length of the string first,
followed by the contents of the string.
Based on your example you are only reading & writing text, I would recommend
using StreamReader & StreamWriter, they specialize in reading & writing text
to a file.
BinaryReader & BinaryWriter specialize in reading & writing stuff in binary
(its native bit format, aka bytes). For example Binary.Write(Integer) will
write four bytes that make up an Integer, BinaryWriter(Long) will write 8
bytes, where as StreamWriter.Write(Integer) will format the Integer as text,
then write the chars that make up that text.
To really see the difference compare the overloaded Write methods on
BinaryWriter:
http://msdn.microsoft.com/library/d.../frlrfSystemIOBinaryWriterClassWriteTopic.asp
With the overloaded Write methods on StreamWriter:
http://msdn.microsoft.com/library/d.../frlrfSystemIOStreamWriterClassWriteTopic.asp
Notice that BinaryWriter has all the "built-in" types, while StreamWriter
only as Char & String.
So using Stream Reader & Writer instead of Binary Reader & Writer you have:
Dim lfsTemp = New FileStream("Original.INI", IO.FileMode.Open)
Dim lbrTemp = New BinaryReader(lfsTemp)
Dim lbTemp = lbrTemp.ReadBytes(lbrTemp.BaseStream.Length)
Dim sFileContent as String =
System.Text.Encoding.Default.GetString(lbTemp)
Dim lbrTemp As New StreamReader("Original.INI", Encoding.Default)
Dim sFileContent As String = lbrTemp.ReadToEnd()
While:
Dim lfsTemp As New FileStream("Copy.Ini", FileMode.CreateNew)
Dim lbwTemp As New BinaryWriter(lfsTemp)
SaveFile = False
lbwTemp.Write(sFileContent)
With a StreamWriter is:
Dim lbwTemp As New StreamWriter("Copy.Ini", False, Encoding.Default)
lbwTemp.Write(sFileContent)
SaveFile = False
Note you can pass the file name directly to the constructor of BinaryReader,
BinaryWriter, StreamReader & StreamWriter. You don't have to create a
FileStream first...
Hope this helps
Jay