I am trying to save an array of FileInfo and DirectoryInfo objects for use as a log file. The goal is to capture the catalog image (and subdirectories) at a specific point in time for subsequent comparison. I am currently using this class to store information:
public class myFSInfo { public FileSystemInfo Dir; public string RelativePath; public string BaseDirectory; public myFSInfo(FileSystemInfo dir, string basedir) { Dir = dir; BaseDirectory = basedir; RelativePath = Dir.FullName.Substring(basedir.Length + (basedir.Last() == '\\' ? 1 : 2)); } private myFSInfo() { }
I tried XML and binary serialization of my class with no luck. I also tried to create a new class that does not contain the actual FileInfo, but only the selected attributes:
public class myFSModInfo { public Type Type; public string BaseDirectory; public string RelativePath; public string FullName; public DateTime DateModified; public DateTime DateCreated; public myFSModInfo(FileSystemInfo dir, string basedir) { Type = dir.GetType(); BaseDirectory = basedir; RelativePath = dir.FullName.Substring(basedir.Length + (basedir.Last() == '\\' ? 1 : 2)); FullName = dir.FullName; DateModified = dir.LastWriteTime; DateCreated = dir.CreationTime; } private myFSModInfo() { }
I also failed to serialize this. I could list the errors that I encountered with my various attempts, but it would probably be easier to choose the best approach. Here is my serialization code:
public void SaveLog(string savepath, string dirpath) { var dirf = new myFSModInfo[1][]; string[] patharr = {dirpath}; GetFSInfo(patharr, dirf); var mySerializer = new System.Xml.Serialization.XmlSerializer(typeof(myFSModInfo[])); var myWriter = new StreamWriter(savepath); mySerializer.Serialize(myWriter, dirf[0]); myWriter.Close(); }
arrays c # xml file serialization
Kalev maricq
source share