C#自動化IO/XML作業

來源:互聯網
上載者:User

標籤:

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

                    }

                }

            }

        }

    }

}

 

C#自動化IO/XML作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.