gpt4 book ai didi

c# - 将 FileSystemInfo 数组保存到文件

转载 作者:数据小太阳 更新时间:2023-10-29 01:48:00 24 4
gpt4 key购买 nike

我正在尝试保存一组 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]); */
}

最佳答案

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

关于c# - 将 FileSystemInfo 数组保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30718482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com