469,326 Members | 1,621 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

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.
Nov 15 '05 #1
3 9679
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)paldino=at=exisconsulting<dot>com

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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.

Nov 15 '05 #2
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 <#d*************@tk2msftngp13.phx.gbl>,
ni**************@exisconsulting.com says...
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.

Nov 15 '05 #3
Nicholas ?......

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

Thanks,
Cybertof.
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Oin Zea | last post: by
9 posts views Thread by greeningster | last post: by
1 post views Thread by Patrick | last post: by
3 posts views Thread by Simon | last post: by
13 posts views Thread by Stuart | last post: by
16 posts views Thread by Claudio Grondi | last post: by
39 posts views Thread by Alan Isaac | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.