You need to look at something called serialization. Heres a simple example
using System;
using System.Runtime.Serialization.Formatter.Binary;
using System.IO;
[Serializable()]
public class MyClass
{
public int MyInt;
private int privateInt;
public MyClass(int pub, int pri)
{
MyInt = pub;
privateInt = pri;
}
public void PrintOut()
{
Console.WriteLine("MyInt = {0}, privateInt = {1}", MyInt.ToString(), privateInt.ToString());
}
}
public class Driver
{
public static void Main()
{
MyClass myClass = new MyClass(4, 2);
// Save the instance to a file
FileStream stream = new FileStream("C:\\\\myclass.bin", FileMode.CreateNew, FileAccess.Write);
IFormatter formatter = new BinaryFormatter();
// Actually does the saving
formatter.Serialize(myClass);
// Close the file
stream.Close();
// Set MyClass to null so that there are no handles to it anymore
myClass = null;
// Read the instance from the file
stream = new FileStream("C:\\\\myclass.bin", FileMode.Open, FileAccess.Read);
// Actually create the class instance from the file
myClass = (MyClass) formatter.Deserialize(stream);
// Close the stream
stream.Close();
// Show that all data was saved, public AND private
myClass.PrintOut();
}
}
If you want to customize what is saved in the serialization process refer to the documentation on the ISerializable interface HTH, James Simplicity Rules!