C#IOXML

实现一个程序,可以将指定的一个文件夹路径下的所有文件夹及文件结构生成一个XML文件,并可以通过此Xml还原文件系统的结构。

要求:

1.      可以通过此Xml文件清晰地看出指定文件夹下,文件夹与文件的层次结构(父子关系),并且能体现出文件的一些属性信息(创建时间,修改时间等)。

2.      根据生成的xml文件,还原此文件结构到指定路径下,并还原相关属性(文件的Content不做要求,可不还原)。

3.      要有比较完善的异常处理机制,假如由于某些原因导致某些文件夹下的文件无法获取,能够跳过继续将剩余的结构写入Xml或还原。

4.      处理过程中要有相应的Log输出,Log输出位置自己指定。例如:

INFO                             9:49:43AM                        :Directory:C:\TestFolder1 was found

INFO                             9:49:43 AM                        :Files:C:\TestFolder1\Test file1.txt was found

INFO                             9:49:43 AM                        :Directory:C:\TestFolder1 \TestFolder2 was found

INFO                             9:49:43 AM                        :Files:C:\TestFolder1\TestFolder2 \Test file2.txt was found

INFO                             9:49:43 AM                        :Directory:C:\TestFolder1\TestFolder2\TestFolder3 was found

ERROR         9:49:44AM                        :Files: C:\TestFolder1\TestFolder2\TestFolder3\Test file3.txt was notfound,Error Message:The process cannotaccess the file ‘C:\TestFolder1\TestFolder2\TestFolder3\Testfile3.txt’ because it is being used by anotherprocess.

异常也要相应的记录在Log之中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
            static void Main(string[] args)
        {
            try
            {
                //Create XML file : You can change target dir  and XML file Save_path(change the Load_path at the same time)
                //target dir
                string str = "C:\\text";
                XmlDocument myDocument = CreateXml(str);
                //XML Save_path
                myDocument.Save("C:\\Users\\administrator.JT\\Desktop\\text.xml");
                //
                //Create Folder according to the XML file in the previous step
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load("C:\\Users\\administrator.JT\\Desktop\\text.xml");
                XmlNodeList xmlNodeList = xdoc.DocumentElement.ChildNodes;
                //Create root file :You can change dir
                string dir = "C:\\Users\\administrator.JT\\Desktop";
                XmlElement root = xdoc.DocumentElement;
                string nextStr = dir + "\\" + root.Name;
                Directory.CreateDirectory(nextStr);
                using (StreamWriter writer = File.AppendText("C:\\LogText.txt"))
                {
                    writer.WriteLine("file  " + root.Name + "   was creat at " + DateTime.Now.ToString() + "  Directory:" + nextStr);
                }
                //create subdirectory
                CreateDirectory(nextStr, xmlNodeList);
            }
            catch (Exception e) { Console.WriteLine(e); }
            Console.ReadKey();
        }

            #region CreateXml
            public static XmlDocument CreateXml(string targetDir)
            {
                    XmlDocument myDocument = new XmlDocument();
                    //add declaration
                    XmlDeclaration declaration = myDocument.CreateXmlDeclaration("1.0", "utf-8", null);
                    myDocument.AppendChild(declaration);
                    //add rootNode
                    XmlElement rootElement = myDocument.CreateElement(targetDir.Substring(targetDir.LastIndexOf("\\") + 1));
                   //output log
                    using (StreamWriter writer = File.AppendText("C:\\LogText.txt"))
                    {
                        writer.WriteLine("file  " + rootElement.Name + "   was find at " + DateTime.Now.ToString() + "  Directory:" + targetDir);
                    }
                    FileInfo file = new FileInfo(targetDir);
                    rootElement.SetAttribute("CreatTime", file.CreationTime.ToString());
                    rootElement.SetAttribute("ModificationTime", file.LastWriteTime.ToString());
                    myDocument.AppendChild(rootElement);
                    #region  add branchNode
                    foreach (string directory in Directory.GetDirectories(targetDir))
                    {
                        XmlElement childElement = myDocument.CreateElement(directory.Substring(directory.LastIndexOf("\\") + 1));
                        //output  log
                        using (StreamWriter writer = File.AppendText("C:\\LogText.txt"))
                        {
                            writer.WriteLine("file  " + childElement.Name + "   was find at " + DateTime.Now.ToString() + "  Directory:" + directory );
                        }
                        FileInfo ifile = new FileInfo(targetDir);
                        childElement.SetAttribute("CreatTime", ifile.CreationTime.ToString());
                        childElement.SetAttribute("ModificationTime", ifile.LastWriteTime.ToString());
                        rootElement.AppendChild(childElement);
 
                        CreateBranch(directory, childElement, myDocument);
                    }
                    #endregion
                    return myDocument;
            }
            #region   descending into subdirectories
            private static void CreateBranch(string targetDir, XmlElement xmlNode, XmlDocument myDocument)
            {
                foreach (string directory in Directory.GetDirectories(targetDir))
                {
                    XmlElement childElement = myDocument.CreateElement(directory.Substring(directory.LastIndexOf("\\") + 1));
                    //output log
                    using (StreamWriter writer = File.AppendText("C:\\LogText.txt"))
                    {
                        writer.WriteLine("file  " + childElement.Name + "  was find at " + DateTime.Now.ToString() + "  Directory:" + directory);
                    }
                    FileInfo file = new FileInfo(targetDir);
                    childElement.SetAttribute("CreatTime", file.CreationTime.ToString());
                    childElement.SetAttribute("ModificationTime", file.LastWriteTime.ToString());
                    xmlNode.AppendChild(childElement);
                    CreateBranch(directory, childElement, myDocument);
                }
            }
            #endregion
            #endregion
            #region  Generate directory according to the XML file
            /// <param name="path">XML Path</param>
        /// <param name="xmlNodeList">xmlNodeList</param>
            public static void CreateDirectory(string path, XmlNodeList xmlNodeList)
            {
                try
                {
                    string nextPath = path;
                    foreach (XmlNode item in xmlNodeList)
                    {
                        nextPath = path + "\\" + item.Name;
                        Directory.CreateDirectory(nextPath);
                        using (StreamWriter writer = File.AppendText("C:\\LogText.txt"))
                        {
                            writer.WriteLine("file  " + item.Name + "  was creat at " + DateTime.Now.ToString() + "  Directory:" + nextPath);
                        }
                        CreateDirectory(nextPath, item.ChildNodes);
                    }
                }
                catch (Exception e) { Console.WriteLine(e); }
            }
            #endregion
    }
}

~~~~~~~~~~~~~~~~

异常没有处理~~~

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值