Convert your data(class,struct..etc) into byte[],then write the byte[] into a file.
Imaging,you have a class like this:
class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
And this is your "data":
List<Customer> customerList = new List<Customer>();
customerList.Add(new Customer
{
FirstName = "Clark ",
LastName = "Kent"
});
Now,let's convert your data into byte[] and write the byte[] into a file.
You need a function to do this for you:
void write(object obj, string path)
{
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress))
{
fs.SetLength(0);
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, obj);
byte[] bytes = ms.ToArray();
zipStream.Write(bytes, 0, bytes.Length);
}
}
Call it like this:
write(customerList, path);
You know what is the "path" here.
This is the way of using your data in your file,just call the "read" function:
List<Customer> customerList = read(path);
The "read" function just like this:
List<Customer> read(string path)
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
GZipStream zipStream = new GZipStream(fs, CompressionMode.Decompress, true);
BinaryFormatter bf = new BinaryFormatter();
return bf.Deserialize(zipStream) as List<Customer>;
}
}