当前位置: 动力学知识库 > 问答 > 编程问答 >

Load a One-Based Array from VB6 Data File in VB.NET?

问题描述:

Say I have a data file that was created in VB6 like this:

Dim arr As Variant

Dim unit As Integer

Dim i As Integer

unit = FreeFile

Open "SomeFile.dat" For Binary As unit

ReDim arr(1 To 10)

For i = 1 To 10

arr(i) = i

Next

Put #unit, , arr

Close #unit

I'm attempting to use the Microsoft.VisualBasic namespace to read the contents of this file by using the FileGetObject routine. However, FileGetObject fails with an IndexOutOfRangeException. My guess is it fails because it cannot handle non-zero-based arrays. Does anyone have a way to read a one-based array from a VB6 data file in .NET?

Here's my VB.NET code:

Imports Microsoft.VisualBasic

Dim mFileNumber as Integer = FreeFile()

FileOpen(mFileNumber, "SomeFile.dat", OpenMode.Binary, OpenAccess.Read)

Dim arr as Object

FileGetObject(mFileNumber, arr)

Note that the actual files I'm reading are much more complicated than this example, as they could contain nested arrays and either 0- or 1-based multidimensional arrays.

Thank you very much for your help,

Kenny

网友答案:

Your issue is not whether the array is a 1 or 0 based array. The array index is not stored in the file, just the array data. If you load the array in C based language it will be a zero-based array (0 to 9). Are you sure tat it is not just getting all the data and then erroring out at the end of the file? Try opening the file in random mode and looping and see where your error occurs:

    Try
        Dim fileHandle As Integer = 1
        Dim c As String
        FileSystem.FileOpen(fileHandle, My.Computer.FileSystem.SpecialDirectories.Desktop & "\test.dat", OpenMode.Random)
        For i = 1 To 10
            FileSystem.Seek(fileHandle, i)
            FileSystem.FileGetObject(fileHandle, c)
            MsgBox(c)
        Next
        FileSystem.FileClose(fileHandle)

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
网友答案:

Any chance you can offer a sample file to play with? I'm guessing any solution will require reading the data into a stream and then coding something to do the conversion.

分享给朋友:
您可能感兴趣的文章:
随机阅读: