Help needed converting VB6 file IO code sample to VB.net

R

Rory Becker

The following code is being used in a VB6 program to read and write a simple
binary file.

I need to move things into the 21st century and recode this in VB.Net

The new VB.Net program will need to continue to operate in the same enterprise
level situation.

Rollout will be slow and so for the moment the new code will have to be able
to read and write these files in the exact same format as the Old VB6 code.

The code is:
-------------------------------------------------------------
Public Type TransferHeader
Hub As String
Version As Long ' I appreciate that this should be an Integer in VB.Net
End Type

Private Sub SetFileHeaderData(Filename As String, Data As TransferHeader)
Dim DataFile As Byte
DataFile = FreeFile()
Open Filename For Binary Access Write As #DataFile
Put #DataFile, , Data
Close #DataFile
End Sub

Private Function GetHeaderOfFile(HeaderFile As String) As TransferHeader
Dim TempHeader As TransferHeader
Dim DataFile As Byte
DataFile = FreeFile()
Open HeaderFile For Binary Access Read As #DataFile
Do Until EOF(DataFile)
Get #DataFile, , TempHeader
If Len(TempHeader.Hub) > 0 Then
GetHeaderOfFile = TempHeader
End If
Loop
Close #DataFile
End Function
 
C

cfps.Christian

While I'm not imminently familiar with VB6 it looks like you need to
look around for the FileStream, StreamWriter, and SteamReader
objects. The StreamReader can open the file and using the
StreamReader.Peek() method you can see if there are any more rows left
(Peek returns -1 if EOF). Then use StreamReader.ReadLine to pull the
data out of the row.

Pretty sure thats what you're looking for.
 
Z

zacks

While I'm not imminently familiar with VB6 it looks like you need to
look around for the FileStream, StreamWriter, and SteamReader
objects.  The StreamReader can open the file and using the
StreamReader.Peek() method you can see if there are any more rows left
(Peek returns -1 if EOF).  Then use StreamReader.ReadLine to pull the
data out of the row.

Pretty sure thats what you're looking for.

Plus the BinaryReader and BinaryWriter classes.
 
R

Rory Becker

While I'm not imminently familiar with VB6 it looks like you need to
Plus the BinaryReader and BinaryWriter classes.

Cheers to you both. I now have a working sample which uses a Binary reader
to retrieve the details from the 1st record in the data. and extract the
Int and the String.

I have managed to resolve the fact that bytes 2 through 6 are the string
I need (All strings will be 4 chars) and the next 2 are the 32 bit integer
I need.

Can someone tell me what the first 2 (values 04 and 00) and last 2 bytes
(values 00 and 00) are for?

Is this to indicate encoding and end of record?

Once I know what these codes mean I should be able to construct the "Writer"
half of the equation.

Thanks once again
 
M

Michel Posseth [MCP]

Last chars i can`t help you with ( strange ) but the first chars are
probably the BOM ( Byte order mark ) afaik you can set this off in the
stream writer
but default it is written as it is a standard to detect the encoding type


HTH

Michel
 
S

Steven Cheng [MSFT]

Hi Rory,

Is the binary file you currently read generated from VB6 app originally? If
so, I think it is probably some placeholder or flag generated by VB file
API. For text files, there does exists some BOM(by order mark) at the
begining, but this is binary file and there is also extra bytes at the file
end, so I don't quite think it is BOM. You can try creating a new binary
file via .NET I/O Stream class and VB6 file api and open them via binary
editor to verify the difference.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
A

Armin Zingler

I think the FileGet etc functions are just there to read VB6 created
files, in order to not bother about translating to ".Net-ish" functions.
Don't know why you make it more complicated than it has to be. I
suggest, use the VB functions to _convert_ the files (to XML or binary
or whatever), then you can easily use the System.IO classes to read them
in the future.


Armin
 
R

Rory Becker

Hello Steven Cheng [MSFT],
Hi Rory,

Is the binary file you currently read generated from VB6 app
originally? If so, I think it is probably some placeholder or flag
generated by VB file API. For text files, there does exists some
BOM(by order mark) at the begining, but this is binary file and there
is also extra bytes at the file end, so I don't quite think it is BOM.
You can try creating a new binary file via .NET I/O Stream class and
VB6 file api and open them via binary editor to verify the difference.

Thanks Steven (and others)

I have done some minor experimentation and discovered that given several
records, the Prefix and Suffix data remains constant for each.

I would almost suggest these might be beginning and end of record markers.

In any case, duplication for each record seems to be working for now .

My thanks to all who have contributed. this has helped a great deal :)
 
S

Steven Cheng [MSFT]

Thanks for your followup Rory,

I'm glad that you've got a working solution and thanks for sharing this
with us.

Have a good day!

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Date: Mon, 28 Apr 2008 10:32:41 +0000 (UTC)
Message-ID: <[email protected]>
From: Rory Becker <[email protected]>
Subject: Re: Help needed converting VB6 file IO code sample to VB.net

Hello Steven Cheng [MSFT],
Hi Rory,

Is the binary file you currently read generated from VB6 app
originally? If so, I think it is probably some placeholder or flag
generated by VB file API. For text files, there does exists some
BOM(by order mark) at the begining, but this is binary file and there
is also extra bytes at the file end, so I don't quite think it is BOM.
You can try creating a new binary file via .NET I/O Stream class and
VB6 file api and open them via binary editor to verify the difference.

Thanks Steven (and others)

I have done some minor experimentation and discovered that given several
records, the Prefix and Suffix data remains constant for each.

I would almost suggest these might be beginning and end of record markers.

In any case, duplication for each record seems to be working for now .

My thanks to all who have contributed. this has helped a great deal :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top