[Serializable]
sealed class DiskFullException : Exception, ISerializable
{
public string diskPath;
public string DiskPath
{
get
{
return diskPath;
}
}
public override string Message
{
get
{
string msg = base.Message;
if (diskPath != null)
{
msg = Environment.NewLine + "Disk Path:" + diskPath;
}
return msg;
}
}
public DiskFullException()
: base()
{
}
public DiskFullException(string Message)
: base(Message)
{
}
public DiskFullException(string message, Exception innerException)
: base(message, innerException)
{
}
private DiskFullException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
diskPath = info.GetString("diskPath");
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("diskPath", diskPath);
base.GetObjectData(info, context);
}
public DiskFullException(string Message, string diskpath)
: this(Message)
{
this.diskPath = diskpath;
}
public DiskFullException(string Message, string diskpath, Exception innerException)
: this(Message, innerException)
{
this.diskPath = diskPath;
}
}
class App
{
static void Main()
{
DiskFullException e = new DiskFullException("The Disk is full", @"C:/");
FileStream fs = new FileStream(@"Test", FileMode.Create);
IFormatter f = new BinaryFormatter();
f.Serialize(fs, e);
fs.Close();
fs = new FileStream(@"Test", FileMode.Open);
e = (DiskFullException)f.Deserialize(fs);
fs.Close();
Console.WriteLine("Type:{1}{0}Diskpath:{2}{0}Message:{3}", Environment.NewLine, e.GetType(), e.diskPath, e.Message);
Console.ReadLine();
}
}