Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to save object of a class to a file.
like --->

classname obj=new classname();

then how to save obj to a file for future use.

and then how to read data back from file in object.
Posted

U r problem can be solver using Serializeand Deserialize concept
see this 1st u should have namespaces


C#
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;



then create a class

C#
[Serializable]
    class Class1
    {
        public string name;
        public string ID;
        public string address;
    }



Serializable is a key word we need to use it

then in main program write this code

BinaryFormatter bnf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();

Class1 obj = new Class1();
obj.name = "Arun";
obj.ID = "007";
obj.address="Mysore";

bnf.Serialize(ms, obj);
byte[] obj_content = ms.ToArray();

//File.Create("C:\\arun.arv");
//File.WriteAllBytes("C:\\arun.arv", obj_content);
FileStream fs1 = new FileStream("C:\\arun.arv", FileMode.Create);
BinaryWriter write = new BinaryWriter(fs1);
write.Write(obj_content);
write.Close();
fs1.Close();

FileStream fs = new FileStream("C:\\arun.arv",FileMode.Open);
BinaryReader read=new BinaryReader (fs);
byte[] obj_converter = new byte[fs.Length];
read.Read(obj_converter, 0, obj_converter.Length);



MemoryStream ms1 = new MemoryStream(obj_converter);
BinaryFormatter bnf1 = new BinaryFormatter();
ms1.Position = 0;

Class1 obj2 = (Class1)bnf1.Deserialize(ms1);
Console.Write(obj2.name);
Console.Write(obj2.ID);
Console.Write(obj2.address);
 
Share this answer
 
If the class you want to serialize isn't marked with [Serializable], you can use XmlSerializer.
 
Share this answer
 
Comments
lavikgupta 10-Nov-11 10:13am    
Class also contains images, so when i use XmlSerializer I got exception Error in creating XML Document.

i use -->

TextWriter ts = new StreamWriter("c:\\a.txt");
XmlSerializer ser = new XmlSerializer(typeof(Man));
ser.Serialize(ts, ser);


Man is a class contains image of person and his name.

Now What to do........ ???
 
Share this answer
 
I know this method....

But i got exception that object is not mark as serializable. so how to convert that object to byte[].

exception is-->
Type '<library_name and="" version="" information="">' is not marked as serializable.
 
Share this answer
 
Comments
lukeer 10-Nov-11 4:06am    
This is what the [Serializable] attribute is for. Decorate the class you want to save with it.
lavikgupta 10-Nov-11 4:20am    
But i does not have the source code of that class......

Its a class in .dll file and i creates its object but now i want to serialize this object but class inside the library is not marked as serializable.

So any solution for this ....
Shmuel Zang 10-Nov-11 4:36am    
If that's the case, see my 2nd solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900