【发布时间】:2009-06-04 18:23:28
【问题描述】:
我有以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication28
{
class Program
{
static void Main()
{
List<string> dirs = FileHelper.GetFilesRecursive(@"c:\Documents and Settings\bob.smith\Desktop\Test");
foreach (string p in dirs)
{
Console.WriteLine(p);
}
//Write Count
Console.WriteLine("Count: {0}", dirs.Count);
Console.Read();
}
static class FileHelper
{
public static List<string> GetFilesRecursive(string b)
{
// 1.
// Store results in the file results list.
List<string> result = new List<string>();
// 2.
// Store a stack of our directories.
Stack<string> stack = new Stack<string>();
// 3.
// Add initial directory.
stack.Push(b);
// 4.
// Continue while there are directories to process
while (stack.Count > 0)
{
// A.
// Get top directory
string dir = stack.Pop();
try
{
// B
// Add all files at this directory to the result List.
result.AddRange(Directory.GetFiles(dir, "*.*"));
// C
// Add all directories at this directory.
foreach (string dn in Directory.GetDirectories(dir))
{
stack.Push(dn);
}
}
catch
{
// D
// Could not open the directory
}
}
return result;
}
}
}
}
上面的代码非常适合递归查找我的 c: 文件夹中的文件/目录。
我正在尝试序列化此代码对 XML 文件所做的结果,但我不确定如何执行此操作。
我的项目是这样的:找到驱动器中的所有文件/目录,序列化为 XML 文件。然后,我第二次运行这个应用程序时,我将有两个 XML 文件进行比较。然后,我想反序列化第一次运行此应用程序时的 XML 文件,并将差异与当前 XML 文件进行比较,并生成更改报告(即已添加、删除、更新的文件)。
我希望能得到一些帮助,因为我是 C# 的初学者,我在序列化和反序列化方面非常不稳定。我在编码时遇到了很多麻烦。有人可以帮我吗?
谢谢
【问题讨论】:
标签: c# serialization xml-serialization