Re: Write class/struct object to file

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi Samuel\Steven,

Yes, I am trying to match the format used by another application. So,
for that I don't think that Binary Serialization will do.

Well I thought .NET made everything easy...but to do this...its
difficult.

Regards,
Ashutosh

Steven Cheng[MSFT] wrote:
Thanks for Sam's input.

Hi Ashutosh,

In .net framework, the code can not directly access memory which is totally different from c/c++. That means you can not write out class/struct objects into memory/file through their point/address. If you want to persist some class objects into memory or file(in binary format), you can use the Binary Serialization feature as Sam suggested.

To do binary serialziation on class, you need to first make the certain class serializable and then use Binary Serializer to serialize the instances of those classes. You can serialize them into memoryStream or fileStream.

Here are some articles introducing .net binary serialization:

#Binary Serialization
http://msdn2.microsoft.com/en-us/library/72hyey7b(VS.71).aspx

#Run-time Serialization http://msdn.microsoft.com/msdnmag/issues/02/04/net/

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: Samuel R. Neff <samuelneff@xxxxxxxxxx>
Subject: Re: Write class/struct object to file
Date: Fri, 25 Jan 2008 17:20:54 -0500


Are you trying to match a format used by another application or simply
store a class/struct as binary data? If the latter, then
BinaryFormatter to Serialize/Deserialize the object to/form the
stream.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.




On Sat, 19 Jan 2008 02:29:29 +0530, "Ashutosh"
<smbs_user@xxxxxxxxxxxxxxxx> wrote:

Hi,
How can I create a fixed size class or structure which consist of members
of
any types and strings of fixed (specified) sizes. I need to write the
class
object by using a BinaryStream....

I need to keep the code fully managed. I tried to declare the
array/string
of fixed size, but the compiler says to make to code unmanaged/unsafe or
use
Marshal attributes. I know how to use those attributes, but as far as I think they doesn't make sense here...

Actually I am looking for something similar to this in C++
---------------------------------------------------------
CFile f;
f.Open(....);

struct someStruct s1;

f.Write((char*)&s1,sizeof(s1));
---------------------------------------------------------
Any ideas how I can achieve the same in C# with minimal effort?

Regards,
Ashutosh



.