Random Access Files in C#

C

Cybertof

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
C

Cybertof

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.
 
C

Cybertof

Nicholas ?......

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

Thanks,
Cybertof.
 

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