2015-08-26 47 views
1

我有一個傳統的VB6應用程序,它以隨機訪問模式寫入文件。這個文件然後由第三方開發的應用程序讀取。VB6到VB.NET寫入隨機訪問文件

我的任務是在VB.NET中重寫VB6應用程序,但第三方應用程序不會更改。我試圖將VB6代碼轉換爲VB.NET,但隨機訪問文件未被正確讀取。

以下提供了VB6和VB.NET代碼的精簡片段。 VB.NET代碼正在成功寫入文件,但是,字段長度不正確,讀取文件的應用程序不能正確解析數據。我怎樣才能以相同的方式寫入隨機訪問文件?

我已經搜索周圍,但還沒有找到可行的解決方案。

VB6

Type Person 
    ID as String * 5 
    Name as String * 25 
    EyeColor as String * 10 
End Type 

Dim myPerson as Person 
myPerson.ID = "13" 
myPerson.Name = "Joe" 
myPerson.EyeColor = "Blue" 

Open <file path> For Random As <file number> Len = Len(myPerson) 
    Put <file number>, myPerson.ID, myPerson 
Close <file number> 

VB.NET

Structure Person 
    <VBFixedString(5)> Dim ID As String 
    <VBFixedString(25)> Dim Name As String 
    <VBFixedString(10)> Dim EyeColor As String 
End Structure 

Dim myPerson as New Person 
myPerson.ID = "13" 
myPerson.Name = "Joe" 
myPerson.EyeColor = "Blue" 

FileOpen(<file number>, <file path>, OpenMode.Random, , , Len(myPerson)) 
    FilePut(<file number>, myPerson, myPerson.ID) 
FileClose(<file number>) 
+0

你查看與十六進制編輯器文件?也許Ansi vs Unicode的東西? –

+0

你應該看看[System.IO](https://msdn.microsoft.com/en-us/library/system.io(v = vs.110).aspx)類,它包含VB.NET函數寫文件而不是使用舊的VB6方法。你不是非常具體的什麼不工作,很難提供幫助。下面是一個猜測:您可以使用[PadRight](https://msdn.microsoft.com/en-us/library/system.string.padright(v = vs.110).aspx)方法來添加額外的空間。 –

+0

正在創建隨機訪問文件,如果我使用.net編寫和讀取/從它們讀取所有函數,它應該如此。根據我迄今爲止的研究,字節級的字段長度似乎是一個問題。 VB6固定字段長度與VB.NET對應字段不一樣 – DonJoe

回答

0

不知道這是否解決了問題編碼....

我在測試硬編碼的FilePut RecordNumber 。

Structure Person 
    <VBFixedString(5)> Dim ID As String 
    <VBFixedString(25)> Dim Name As String 
    <VBFixedString(10)> Dim EyeColor As String 
End Structure 

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    Dim myPerson As New Person 
    myPerson.ID = "13".PadLeft(5) 
    myPerson.Name = "Joe".PadLeft(15) 
    myPerson.EyeColor = "Blue".PadLeft(10) 
    Stop 

    FileOpen(1, "C:\temp\random.bin", OpenMode.Random, , , Len(myPerson)) 
    FilePut(1, myPerson, 2) ' needs integer RecordNumber - depreciated? 
    FileClose(1) 
End Sub 

Visual Studio中有一個十六進制編輯器 - 我認爲是下文件的選項,打開