Bayu Anggara
Bayu Anggara

Reputation: 99

Read binary file position by position and convert it?

I Know on VB we can read binary file using this code

    Function GetMonData()
            Dim Header(63) As Byte, Rows As Long, NoUse As Long
            Dim i As Long, j As Long, TmpStr As String

            Open "file.dat" For Binary As #1
                Get #1, , Header
                Get #1, , Rows
                Get #1, , NoUse       
            Close #1
    End Function

But how about method in c# ? especially Get #1, , Header I already try

string strFilePath = @"C:\file.dat";
FileStream stream = new FileStream(strFilePath, FileMode.Open);
BinaryReader b = new BinaryReader(File.Open(strFilePath, FileMode.Open));

I just confused to get data (63) byte for header , (4) byte for Rows, (4) byte for NoUse in VB we can use Get #1, , Header, What about c# ? i need to Seek for the stream ?

Thanks in advanced

Upvotes: 0

Views: 385

Answers (1)

Hans Passant
Hans Passant

Reputation: 941208

That is VB6/VBA code. The olden syntax is still supported in VB.NET, grudgingly, to permit porting programs. But surely you'll have to change the declarations from Long to Integer.

If you need to be able to read old files like this then the most obvious way to do it is to take advantage of .NET's excellent language interop and create a VB.NET class library that you reference in your C# project. By far the best way to ensure that the code is compatible and can deal with the weirdo semantics of Get().

Otherwise you'll have to use BinaryReader.GetBytes() to read Header, ReadInt32() to get the other ones.

Upvotes: 2

Related Questions