如何创建和保存包含指定文件夹的完整文件和文件夹层次结构的XML文件?

这是我在网站上的第一篇post:)

所以基本上我需要一个gui应用程序,它可以创建和保存包含指定文件夹的完整文件和文件夹层次结构的XML文件。

1.每个文件夹都应符合:文件夹名称,文件夹大小(字节)和文件数量。

2.每个文件都应该符合:文件名,文件大小(字节),文件创建,文件上次访问时间,文件上次修改时间。

创建XML文件后,应用程序需要显示整个文件夹层次结构树(使用TreeView类)。

有人可以提供帮助和解答吗? 谢谢!

请尝试以下代码。 经过全面测试 从小目录开始。 非常大的文件夹可能需要时间。 我更新了代码以加快加载树视图。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Xml; using System.Xml.Linq; namespace WindowsFormsApplication29 { public partial class Form1 : Form { XDocument doc = null; public Form1() { InitializeComponent(); folderBrowserDialog1.SelectedPath = @"c:\temp"; } private void buttonBrowseForFolder_Click(object sender, EventArgs e) { folderBrowserDialog1.ShowDialog(); textBoxFolderName.Text = folderBrowserDialog1.SelectedPath; } private void buttonCreateXml_Click(object sender, EventArgs e) { if(Directory.Exists(textBoxFolderName.Text)) { string header = " "; doc = XDocument.Parse(header); XElement root = doc.Root; CreateXmlRecursive(textBoxFolderName.Text, root); } } private float CreateXmlRecursive(string folder, XElement folderElement) { folderElement.SetValue(folder); DirectoryInfo dInfo = new DirectoryInfo(folder); int numberOfFiles = 0; float size = 0.0f; foreach(FileInfo fInfo in dInfo.GetFiles()) { try { float fSize = fInfo.Length; size += fSize; folderElement.Add(new XElement("File", new object[] { new XAttribute("size",fSize), new XAttribute("creationDate", fInfo.CreationTime.ToShortDateString()), new XAttribute("lastAccessDate", fInfo.LastAccessTime.ToShortDateString()), new XAttribute("lastModifiedDate", fInfo.LastWriteTime.ToShortDateString()), fInfo.Name })); numberOfFiles += 1; } catch(Exception e) { Console.WriteLine("Error : CAnnot Access File '{0}'", fInfo.Name); } } foreach(string subFolder in Directory.GetDirectories(folder)) { XElement childDirectory = new XElement("Directory"); folderElement.Add(childDirectory); float dSize = CreateXmlRecursive(subFolder, childDirectory); size += dSize; } folderElement.Add(new XAttribute[] { new XAttribute("size", size), new XAttribute("numberOfFiles", numberOfFiles) }); return size; } private void buttonCreateTree_Click(object sender, EventArgs e) { if (doc != null) { TreeNode rootNode = new TreeNode(doc.Root.FirstNode.ToString()); AddNode(doc.Root, rootNode); treeView1.Nodes.Add(rootNode); treeView1.ExpandAll(); } } private void AddNode(XElement xElement, TreeNode inTreeNode) { // An element. Display element name + attribute names & values. foreach (var att in xElement.Attributes()) { inTreeNode.Text = inTreeNode.Text + " " + att.Name.LocalName + ": " + att.Value; } // Add children foreach (XElement childElement in xElement.Elements()) { TreeNode tNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(childElement.Value))]; AddNode(childElement, tNode); } } } } 

你的问题是 – 你可以为我做我的申请 – 但无论如何。

我会给你一些提示来开始你的项目。

首先 – 在这里查看MVVM 。 这将帮助您 – 处理WPF

1.选择起始文件夹

然后,您将需要一个FolderPicker来启动您的搜索

  public static string PickFolder() { var dialog = new System.Windows.Forms.FolderBrowserDialog(); System.Windows.Forms.DialogResult result = dialog.ShowDialog(); string folder = string.Empty; switch (result) { case System.Windows.Forms.DialogResult.OK: return dialog.SelectedPath; case System.Windows.Forms.DialogResult.Cancel: return string.Empty; default: return string.Empty; } } 

你将需要System.Windows.Forms程序集。 (项目 – >添加参考 – >组装)

2.文件夹和文件

然后你想要遍历所有文件夹。

在这里查看System.IO.Directory

3.文件信息

在这里查看System.IO.File – 这将为您提供一些文件数据并获取文件大小来检查这一点