可以根据XML恢复目录至指定地点 创建的文件为空
xml如下
<?xml version="1.0" encoding="utf-8"?>
<directory_root directory_fullname="C:\Users\nhuang\Documents\学习资料">
<folder name="图书资料" directory_modify_time="2016/8/17 8:13:22">
<file name="C#高级编程(第7版).pdf" file_modify_time="2011/11/6 18:45:28">70b7d7f2d61ddaa4019ce9b89cbd4eb6</file>
<file name="CLR via C# 第4版.pdf" file_modify_time="2016/8/15 17:24:00">b893e5a6d0196472a97a5d131c36546b</file>
</folder>
<folder name="视频资料" directory_modify_time="2016/8/18 13:15:21">
<folder name="New folder" directory_modify_time="2016/8/18 13:15:29">
<file name="New Text Document.txt" file_modify_time="2016/8/18 15:01:15">no access</file>
</folder>
<file name="IT行为规范.wmv" file_modify_time="2016/8/15 16:24:46">f6b7bd84ee5cb046efc84e1ede45292c</file>
<file name="邮件书写规范.wmv" file_modify_time="2016/8/16 10:15:06">bab1502343ac95f1ecfe9b95d13889c0</file>
</folder>
<file name="c#中级培训 WFC.pptx" file_modify_time="2016/8/18 10:59:32">d41d8cd98f00b204e9800998ecf8427e</file>
<file name="c#中级培训 异常处理.pptx" file_modify_time="2016/8/18 10:59:32">d41d8cd98f00b204e9800998ecf8427e</file>
<file name="c#初级培训 IO与XML.pptx" file_modify_time="2016/8/18 10:59:32">d41d8cd98f00b204e9800998ecf8427e</file>
<file name="c#初级培训 数据库编程.pptx" file_modify_time="2016/8/18 10:59:32">d41d8cd98f00b204e9800998ecf8427e</file>
</directory_root>
c#函数
public static void RestoreAllFilesFormXML(string RestoreFilePath, string sourceXmlFilePath, XmlDocument xmlDoc, XmlNode rootNode)
{
string fullName = null;//XML存储的信息的位置
string rootName = null;
if(rootNode == null)
{
xmlDoc = new XmlDocument();
xmlDoc.Load(sourceXmlFilePath);
rootNode = xmlDoc.SelectSingleNode(@"/directory_root");
fullName = rootNode.Attributes["directory_fullname"].Value;
Console.WriteLine("FullName: " + fullName);
DirectoryInfo rootDirectory = new DirectoryInfo(fullName);
rootName = rootDirectory.Name;//得到该文件夹的名字
Console.WriteLine("RootName: " + rootName);
DirectoryInfo restoreDirectory = new DirectoryInfo(RestoreFilePath);
restoreDirectory.CreateSubdirectory(rootName);//创建根目录
RestoreFilePath = RestoreFilePath +"\\"+ rootName;
}
else
{
fullName = RestoreFilePath;
DirectoryInfo rootDirectory = new DirectoryInfo(fullName);
rootName = rootDirectory.Name;//得到该文件夹的名字
Console.WriteLine("FullName2: " + fullName);
}
XmlNodeList subFolder = rootNode.SelectNodes("folder");//XML中子文件夹集合
foreach(XmlNode node in subFolder)
{
string subFolderbName = node.Attributes["name"].Value;
Console.WriteLine("subFolderbName: " + subFolderbName);
DirectoryInfo subRestoreDirectory = new DirectoryInfo(RestoreFilePath);
subRestoreDirectory.CreateSubdirectory(subFolderbName);//创建文件夹
string subRestorePath = RestoreFilePath + "\\" + subFolderbName;
Console.WriteLine("subRestorePath: " + RestoreFilePath + "\\" + subFolderbName);
RestoreAllFilesFormXML(subRestorePath, sourceXmlFilePath, xmlDoc, node);//递归 恢复该文件夹下的文件
}
XmlNodeList subFile = rootNode.SelectNodes("file");//XML中子文件集合
foreach (XmlNode node in subFile)
{
string fileName = node.Attributes["name"].Value;
Console.WriteLine("subFileName: " + fileName);
Console.WriteLine(RestoreFilePath + "\\" + rootName + "\\" + fileName);
File.Create(RestoreFilePath + "\\" + "\\" + fileName);//创建文件
}
}
main函数调用
public static string LogPath = @"C:\Users\nhuang\Desktop\mylog.txt";
public static string DirectoryPath = @"C:\Users\nhuang\Documents\学习资料";
public static string DestinationXMLPath = @"C:\Users\nhuang\Desktop\a.xml";
public static string RestoreFilePath = @"C:\Users\nhuang\Desktop";
internal static void Main()
{
try
{
XmlNode tempNode = null;
XmlDocument xmlDoc = new XmlDocument();
RestoreAllFilesFormXML(RestoreFilePath, DestinationXMLPath, xmlDoc, tempNode);
}
catch (InvalidOperationException)
{
// 异常处理程序
}
catch (IOException ex)
{
MyLog(LogPath, "Error " + System.DateTime.Now.ToLongTimeString() + " :" + ex.Message);//ErrorLog
}
catch
{
throw;
}
finally
{
}
}