Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. VB6 --> C# Binary file reading

VB6 --> C# Binary file reading

Scheduled Pinned Locked Moved C#
csharpdata-structuresquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aldogon
    wrote on last edited by
    #1

    Hi, this is the code i've been trying to convert from VB6 to C#...Is there any way to avoid reading every field of the struct and reaing the whole thing? VB6 code: -------------------------------------------------------- Type Person sName as String * 12 Age as Byte Code as Long End Type private persons() as Person Sub LoadData(path As String) dim hf as long hf = FreeFile dim recs as long dim offset as long open path for binary access read lock write as #hf get #hf, 1, recs get #hf, , offset if recs > 0 redim persons(1 to recs) '' Very easy way to retrieve UDT array data get #hf, offset + 1, persons() end if End Sub C#: ------------------------------------------------------- using System; using System.IO; using System.Runtime.InteropServices; //[StructLayout(LayoutKind.Sequential)] public struct Person { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)] string Name; byte Age; int Code; } public class PersonReader { private Person[] persons; private void LoadData(string path) { Stream fileContents = File.Open(path, FileMode.Open); BinaryReader binReader = new BinaryReader(fileContents); int recs = binReader.ReadInt32(); int offset = binReader.ReadInt32(); binReader.BaseStream.Position = offset; //Something like this should go here: persons = binReader(...//read till the eof and convert it to Person[] :P // Instead of a code that reads each data of the struct binReader.Close(); fileContents.Close(); } }

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups