RobinAtTech
RobinAtTech

Reputation: 1329

Binary file writing the same data format in VB6 and C#

I have found an issue. I have to write binary files by using VB6 and C#. When I wrote same set of variables as mentioned below, both output differs. I can guess, the way they represent the string is different in C# and VB6. I just wanted to know is there any way to make both writing similar. Because there are timesI may need to write the same contents by C# and VB

VB6

nFileNum = FreeFile
stringVal = ""
stringVal2 = "Hello"
i = 25
sFilename = "C:\Temp\fromVB.bin"
Open sFilename For Binary As #nFileNum
    Put #nFileNum, , stringVal
    Put #nFileNum, , stringVal2
    Put #nFileNum, , i
Close #nFileNum

C#

    const string fileName = @"C:\Temp\fromC#.bin";
    string stringVal = "";
    string stringVal2 = "Hello";
    int i = 25;

    using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
    {
        writer.Write(stringVal);
        writer.Write(stringVal2);
        writer.Write(i);
    }

Upvotes: 2

Views: 2149

Answers (2)

Deanna
Deanna

Reputation: 24283

I can't remember the exact format of the data written by VB6 (and I'm too lazy to test) but you may need to write out the string length and the string itself seperately. You should be able to see the exact format using a hex editor to view the files.

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 416121

That's hardly the same code. You shouldn't be using the old File Number -based IO in VB.Net any more. That API still exists for the sole purpose of making it easier to port code to .Net from VB6. VB.Net should be using the same API as the C# code. All the more so if you want matching results:

Const fileName As String = "C:\Temp\fromVB.bin"
Dim stringVal As String = ""
Dim stringVal2 As String = "Hello"
Dim i As Integer = 25

Using writer As New BinaryWriter(File.Open(fileName, FileMode.Create))
    writer.Write(stringVal)
    writer.Write(stringVal2)
    writer.Write(i)
End Using

Upvotes: 2

Related Questions