C # Automation Io/xml jobs

Source: Internet
Author: User

PS: This is our company's automated testing to stay a job, although I am not automated, but also did a bit.

Friday, November 28, 2014

? This is also my own according to the automation Department of the work of the analysis written, did not write the process of log, the refinement of the time to judge again, the main purpose is to learn C #. This time the content is a form program: traverse the file under the specified directory, the file information into XML, the path structure in the XML is restored to the specified directory, if the extended write can also be backup and restore data.

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"));

}

}

}

}

}

}

C # Automation Io/xml jobs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.