Hi there,
You know of serializing one class, or rather one object. Then you say, there are multiple files to be written into one file, hence you cannot use that approach. I'd say you just know the answer and how to do it (or rather you possess the capacity to do it) but you are way
too lazy to do it.
No offense, but if that is the case, development is not for you :) Anyways, here you go, it just took me 3 minutes to write this code up and 1 search in Google to know what I would want :S Frankly, typing and formatting this answer took me longer :D lol
First off all, an overview of the answer posted below: All I have done is put all information of the files that I want into one class and SERIALIZE that class. Yes, simple like that.
using System;
using System.Runtime.Serialization;
namespace Objects.Serialization
{
[Serializable()]
class SelectedFileInfo : ISerializable
{
#region Variable(s) & Propert(y/ies)
public string fileName;
public byte[] fileData;
#endregion
#region Constant(s)
private const string FileNameField = "FileName";
private const string FileDataField = "FileData";
#endregion
#region Constructor(s)
public SelectedFileInfo()
{
this.fileName = string.Empty;
this.fileData = null;
}
public SelectedFileInfo(SerializationInfo info, StreamingContext context)
{
this.fileName = info.GetString(SelectedFileInfo.FileNameField);
this.fileData = (byte[])info.GetValue(SelectedFileInfo.FileDataField, typeof(byte[]));
}
#endregion
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
this.retrieveObjectData(info, context);
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
this.retrieveObjectData(info, context);
}
private void retrieveObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(SelectedFileInfo.FileNameField, this.fileName);
info.AddValue(SelectedFileInfo.FileDataField, this.fileData);
}
#endregion
}
}
This class is what actually contains the data of the file you want, I've only put 2 members in the class, but you can add or remove anything you want, based on your needs. Create an object of this class and put data into it for each file that you have (So if you have, 5 files >> 5 objects, 2 files >> 2 objects, etc.).
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Objects.Serialization
{
[Serializable()]
class SelectedFileHolder : ISerializable
{
#region Variable(s) & Propert(y/ies)
public List<selectedfileinfo> fileList;
#endregion
#region Constant(s)
private const string FileListField = "FileList";
#endregion
#region Constructor(s)
public SelectedFileHolder()
{
this.fileList = new List<selectedfileinfo>();
}
public SelectedFileHolder(SerializationInfo info, StreamingContext context)
{
this.fileList = (List<selectedfileinfo>)info.GetValue(SelectedFileHolder.FileListField, typeof(List<selectedfileinfo>));
}
#endregion
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
this.retrieveObjectData(info, context);
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
this.retrieveObjectData(info, context);
}
private void retrieveObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(SelectedFileHolder.FileListField, this.fileList);
}
#endregion
}
}
This is the class which would contain all the
SelectedFileInfo
objects you created and it is this class that we are going to serialize and de-serialize. I.e. the
SelectedFileInfo
objects that created for each file are added to an instance of this class.
If you still don't get it, look into the example I've created below.
SelectedFileHolder holder = new SelectedFileHolder();
SelectedFileInfo file1 = new SelectedFileInfo();
file1.fileData = File.ReadAllBytes(@"C:\wamp\license.txt");
file1.fileName = "License Data";
holder.fileList.Add(file1);
MessageBox.Show(string.Format("{0} : {1}", holder.fileList[0].fileName, holder.fileList[0].fileData.Length));
Stream writeStream = File.Open(Form1.SaveFilePath, FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(writeStream, holder);
writeStream.Close();
Stream readStream = File.Open(Form1.SaveFilePath, FileMode.Open);
bformatter = new BinaryFormatter();
SelectedFileHolder newHolder = (SelectedFileHolder)bformatter.Deserialize(readStream);
writeStream.Close();
MessageBox.Show(string.Format("{0} : {1}", newHolder.fileList[0].fileName, newHolder.fileList[0].fileData.Length));
Since you said you know I will not be explaining about "Serializing". In case you need help with that refer to this
article[
^]. It got everything you need to know to achieve what you need and my solution is based on that article too.
Hope this helps :) Regards