将FileSystemInfo数组保存到文件

时间:2015-06-08 20:28:33

标签: c# arrays xml file serialization

我试图保存一个FileInfo和DirectoryInfo对象数组,以用作日志文件。目标是在某个时间点捕获目录(和子目录)的映像,以便以后进行比较。我目前正在使用此类来存储信息:

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() { }
    /// <summary>
    /// Copies a FileInfo or DirectoryInfo object to the specified path, creating folders and overwriting if necessary.
    /// </summary>
    /// <param name="path"></param>
    public void CopyTo(string path)
    {
        if (Dir is FileInfo)
        {
            var f = (FileInfo)Dir;
            Directory.CreateDirectory(path.Substring(0,path.LastIndexOf("\\")));
            f.CopyTo(path,true);
        }
        else if (Dir is DirectoryInfo) Directory.CreateDirectory(path);
    }
}

我已经尝试过XML和二进制序列化我的课程而没有运气。我还尝试创建一个不包含实际FileInfo但只包含选定属性的新类:

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() { }
    /// <summary>
    /// Copies a FileInfo or DirectoryInfo object to the specified path, creating folders and overwriting if necessary.
    /// </summary>
    /// <param name="path"></param>
    public void CopyTo(string path)
    {
        if (Type == typeof(FileInfo))
        {
            Directory.CreateDirectory(path.Substring(0, path.LastIndexOf("\\")));
            File.Copy(FullName,path, true);
        }
        else if (Type == typeof(DirectoryInfo)) Directory.CreateDirectory(path);
    }
    public void Delete() 
    {
        if (Type == typeof(FileInfo)) File.Delete(FullName);
        else if (Type == typeof(DirectoryInfo)) Directory.Delete(FullName);
    }
}

我也没有运气序列化这个。我可以列出我在各种尝试中遇到的错误,但是首先选择最佳方法可能会更容易。这是我的序列化代码:

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();

        /*var bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();   
        FileStream fs = new FileStream(savepath, FileMode.Create, FileAccess.Write);   
        bf.Serialize(fs, dirf[0]);  */
    }

1 个答案:

答案 0 :(得分:5)

FileSystemInfo不可序列化,因为它不是simple type。 FileInfo不可序列化,因为它没有空default constructor

因此,如果要保存该信息,则必须使用简单类型构建自己的类,这些类包装来自FileInfo或FileSystemInfo的信息。

[Serializable]
public class MyFileInfo
{
    public string Name { get; set; }

    public long Length { get; set;}

    /// <summary>
    /// An empty ctor is needed for serialization.
    /// </summary>
    public MyFileInfo(){
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="test.MyFileInfo"/> class.
    /// </summary>
    /// <param name="fileInfo">File info.</param>
    public MyFileInfo(string path)
    {
        FileInfo fileInfo = new FileInfo (path);
        this.Length = fileInfo.Length;
        this.Name = fileInfo.Name;
        // TODO: add and initilize other members
    }
}

使用示例:

List<MyFileInfo> list = new List<MyFileInfo> ();

foreach (string entry in Directory.GetFiles(@"c:\temp"))
{
    list.Add (new MyFileInfo (entry));
}

XmlSerializer xsSubmit = new XmlSerializer(typeof(List<MyFileInfo>));
StringWriter sww = new StringWriter();
XmlWriter writer = XmlWriter.Create(sww);
xsSubmit.Serialize(writer, list);

Console.WriteLine (sww.ToString());