Random Access Files in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cybertof

    #1

    Random Access Files in C#

    Hello,

    Is there a simple way to read a random access file that has been created
    with VB6 using direct writing to disk of Type....End Type structures ?
    I have not found this possibility in C#.

    Is it possible to map directly a buffer (read from a stream) to a
    Structure in memory ?
    If yes, how to do this ?
    If no, any hint to read/access/write random access files ?


    I would appreciate any advice or comment.
    Regards,
    Cybertof.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Random Access Files in C#

    Cybertof,

    This is not supported in .NET. If you want to do something like this in
    ..NET, then you will find it easiest if you use unsafe code (so that you may
    access pointers in memory) as well as calls to the appropriate APIs to
    create open the file and write the contents from memory.

    If you don't want to go so far, you could declare your structures as if
    you were marshaling them across the unmanaged/managed boundary. Then, you
    could use the static SizeOf method on the Marshal class to figure out how
    many bytes to read from the underlying stream. Once you have those bytes,
    you can copy them to unmanaged memory, and then call the static
    PtrToStructure method on the Marshal class to read from the unmanaged memory
    to your structure. Of course, this will be less performant than the first
    option, but it would not require the use of unsafe code.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
    news:MPG.19ef57 13416b86e998969 e@msnews.micros oft.com...[color=blue]
    > Hello,
    >
    > Is there a simple way to read a random access file that has been created
    > with VB6 using direct writing to disk of Type....End Type structures ?
    > I have not found this possibility in C#.
    >
    > Is it possible to map directly a buffer (read from a stream) to a
    > Structure in memory ?
    > If yes, how to do this ?
    > If no, any hint to read/access/write random access files ?
    >
    >
    > I would appreciate any advice or comment.
    > Regards,
    > Cybertof.[/color]


    Comment

    • Cybertof

      #3
      Re: Random Access Files in C#

      Nicholas,

      Thanks for these first elements.
      I would be interested in knowing a little more about your 2 options.

      Do you have any sample code / info regarding
      - using unsafe code (to access pointers)
      - marshaling structure regarding unmanaged/managed boundary.
      (btw.....what does 'marshaling' means ?)

      When you say "as well as calls to the appropriate APIS to
      create/open/write the contents from memory".
      Do you mean than with mixing unsage-code and safe-code it is not
      possible to use at the same time the .net native streams to
      create/write/read files ?


      Cybertof.


      In article <#dnLb1mjDHA.74 4@tk2msftngp13. phx.gbl>,
      nicholas.paldin o@exisconsultin g.com says...[color=blue]
      > Cybertof,
      >
      > This is not supported in .NET. If you want to do something like this in
      > .NET, then you will find it easiest if you use unsafe code (so that you may
      > access pointers in memory) as well as calls to the appropriate APIs to
      > create open the file and write the contents from memory.
      >
      > If you don't want to go so far, you could declare your structures as if
      > you were marshaling them across the unmanaged/managed boundary. Then, you
      > could use the static SizeOf method on the Marshal class to figure out how
      > many bytes to read from the underlying stream. Once you have those bytes,
      > you can copy them to unmanaged memory, and then call the static
      > PtrToStructure method on the Marshal class to read from the unmanaged memory
      > to your structure. Of course, this will be less performant than the first
      > option, but it would not require the use of unsafe code.
      >
      > Hope this helps.
      >
      >[/color]

      Comment

      • Cybertof

        #4
        Re: Random Access Files in C#

        Nicholas ?......

        If you read me if you could put a short reply :=)

        Thanks,
        Cybertof.

        Comment

        Working...