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

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

本文介绍了如何创建和保存包含指定文件夹的文件和文件夹的完整层次结构的XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

this is my first post here on the site :)

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

So basically I need a gui application that can create and save XML file containing complete hierarchy of files and folders for a specified folder.

1.每个文件夹都应使用以下名称进行限定:文件夹名称,文件夹大小(字节)和文件数.

1.Each folder should be qualified with: Folder name, Folder size (bytes) and Number of files.

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

2.Each file should be qualified with: File name, File size (bytes), File creation, File last access time, File last modified time.

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

After the XML file is created the application needs to display the entire folder hierarchy tree (by using the TreeView class).

任何人都可以提供帮助和答案吗?谢谢!

Can anyone provide help and answer? Thanks!

推荐答案

尝试以下代码.经过全面测试.从小目录开始.很大的文件夹可能需要一些时间.我更新了代码以加快树形视图的加载速度.

Try following code. Fully tested. Start with small Directory. Very large folders may take time. I updated code to speed up loading the 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 = "<?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);
            }
        }

    }
}

这篇关于如何创建和保存包含指定文件夹的文件和文件夹的完整层次结构的XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:42