C#自动化IO/XML作业

时间:2024-06-24 13:04:08

PS:这是我们公司自动化测试留的一个作业,虽然我不是自动化的,但是也做了一下。

Friday, November 28, 2014

​这个也是我根据自动化部门的那次作业自己分析写的,没有写打log的过程,细化的时候再判断再分析吧,主要目的是学习C#。本次的内容是一个窗体程序:遍历指定目录下的文件;将文件信息存入XML中;将XML中的路径结构还原到指定目录下;如果扩展写的话还可以进行数据的备份和还原。

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.Text.RegularExpressions;

namespace WindowsFormsApplication2

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

//Blank

}

private void BackUp_Click(object sender, EventArgs e)

{

string path = CheckOutPath.Text.ToString();

//Create XML

CreateXML();

GetAllFiles(path);

}

//Get all files under the path

private void GetAllFiles(string path)

{

DirectoryInfo dir = new DirectoryInfo(@path);

FileInfo[] files = dir.GetFiles();

//Store files into XML

StoreIntoXML(dir.Parent.ToString(), files);

DirectoryInfo[] subDirs = dir.GetDirectories();

//Store subdirs into XML

StoreIntoXML(dir.Parent.ToString(),subDirs);

foreach (DirectoryInfo subDir in subDirs) {

string subPath = subDir.FullName;

GetAllFiles(subPath);

}

}

//Create the XML under the XMLForBackUpPath

private void CreateXML()

{

string XMLPath = XMLForBackUpPath.Text.ToString();

XmlDocument xml = new XmlDocument();

xml.LoadXml("<XML></XML>");

string filePath = Path.Combine(XMLPath,"BackUp.XML");

xml.Save(@filePath);

}

//Store subdirs into XML

private void StoreIntoXML(string parentDir,DirectoryInfo[] subDirs)

{

//Load the XML

string XMLPath = XMLForBackUpPath.Text.ToString();

string filePath = Path.Combine(XMLPath, "BackUp.XML");

XmlDocument xml = new XmlDocument();

xml.Load(@filePath);

//Append the child node to parentDir if possible

foreach (DirectoryInfo subDir in subDirs)

{

XmlElement subNode = xml.CreateElement("FolderNode");

xml.DocumentElement.AppendChild(subNode);

subNode.SetAttribute("type", "folder");

subNode.SetAttribute("path", subDir.FullName.ToString());

subNode.SetAttribute("name", subDir.ToString());

subNode.SetAttribute("parent", parentDir);

}

xml.Save(@filePath);

}

//Store files into XML

private void StoreIntoXML(string parentDir,FileInfo[] files)

{

//Load the XML

string XMLPath = XMLForBackUpPath.Text.ToString();

string filePath = Path.Combine(XMLPath, "BackUp.XML");

XmlDocument xml = new XmlDocument();

xml.Load(@filePath);

//Append the child node to parentDir if possible

foreach (FileInfo file in files)

{

XmlElement subNode = xml.CreateElement("FileNode");

xml.DocumentElement.AppendChild(subNode);

subNode.SetAttribute("type", "file");

subNode.SetAttribute("name", file.ToString());

subNode.SetAttribute("path", file.FullName.ToString());

subNode.SetAttribute("parent",parentDir);

}

xml.Save(@filePath);

}

private void Restore_Click(object sender, EventArgs e)

{

//Restore

string RestorePath=XMLForRestorePath.Text.ToString();

RestoreSolution(RestorePath);

}

//Restore the file system structure from the XML

private void RestoreSolution(string path)

{

XmlDocument XMLForRestore = new XmlDocument();

XMLForRestore.Load(@path);

XmlNodeList nodeLists = XMLForRestore.DocumentElement.ChildNodes;

foreach(XmlElement ele in nodeLists)

{

if (ele.GetAttribute("type") == "folder")

{

if (!System.IO.Directory.Exists(@ele.GetAttribute("path")))

{

Directory.CreateDirectory(@ele.GetAttribute("path"));

}

}

}

}

}

}