The tricky bit in VB6 was that you were allowed to declare structures with fixed length strings so you can write records containing strings that don't need a length prefix. The length of the string buffer was encoded into a type, and should not be written to the record. This allowed us to record fixed size recordings. In .NET, this is partly left in the sense that VB.NET has a mechanism to support it for backward compatibility, but it is not intended for C #, as far as I can tell: How to declare a fixed-length string in VB.NET? .
It is assumed that .NET usually prefers to write strings with a prefix of length, which means that records usually have a variable length. This is suggested by implementing BinaryReader.ReadString .
However, you can use System.BitConverter for finer control over how records are serialized and de-serialized as bytes (System.IO.BinaryReader and System.IO.BinaryWriter are probably not useful as they make assumptions about strings have a prefix of length). Keep in mind that VB6 Integer maps to .NET Int16 and VB6 Long is .Net Int32. I donβt know exactly how you defined your VB6 structure, but here is one possible implementation:
class Program { static void Main(string[] args) { WpRecType[] WpRec = new WpRecType[3]; WpRec[0] = new WpRecType(); WpRec[0].WpIndex = 0; WpRec[0].WpName = "New York"; WpRec[0].WpLat = 40.783f; WpRec[0].WpLon = 73.967f; WpRec[0].WpLatDir = 1; WpRec[0].WpLonDir = 1; WpRec[1] = new WpRecType(); WpRec[1].WpIndex = 1; WpRec[1].WpName = "Minneapolis"; WpRec[1].WpLat = 44.983f; WpRec[1].WpLon = 93.233f; WpRec[1].WpLatDir = 1; WpRec[1].WpLonDir = 1; WpRec[2] = new WpRecType(); WpRec[2].WpIndex = 2; WpRec[2].WpName = "Moscow"; WpRec[2].WpLat = 55.75f; WpRec[2].WpLon = 37.6f; WpRec[2].WpLatDir = 1; WpRec[2].WpLonDir = 2; byte[] buffer = new byte[WpRecType.RecordSize]; using (System.IO.FileStream stm = new System.IO.FileStream(@"C:\Users\Public\Documents\FplDb.dat", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)) { WpRec[0].SerializeInto(buffer); stm.Write(buffer, 0, buffer.Length); WpRec[1].SerializeInto(buffer); stm.Write(buffer, 0, buffer.Length); WpRec[2].SerializeInto(buffer); stm.Write(buffer, 0, buffer.Length);
Bluemonkmn
source share