这是我在网站上的第一篇帖子:)
所以基本上我需要一个gui应用程序,它可以创建和保存包含指定文件夹的完整文件和文件夹层次结构的XML文件。
1.每个文件夹都应符合:文件夹名称,文件夹大小(字节)和文件数。
2.每个文件都应该符合:文件名,文件大小(字节),文件创建,文件上次访问时间,文件最后修改时间。
创建XML文件后,应用程序需要显示整个文件夹层次结构树(使用TreeView类)。
任何人都可以提供帮助和解答吗?谢谢!
答案 0 :(得分:1)
你的问题是 - 你可以为我申请 - 但无论如何。
我会给你一些提示来开始你的项目。
首先 - 查看MVVM
here。这将有助于您处理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
大会。 (项目 - &gt;添加参考 - &gt;装配)
<强> 2。文件夹和文件
然后你想要遍历所有文件夹。
结帐System.IO.Directory
here
第3。文件信息
答案 1 :(得分:1)
尝试以下代码。经过全面测试从小目录开始。非常大的文件夹可能需要时间。我更新了代码以加快加载树视图。
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 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Directory></Directory> ";
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);
}
}
}
}