zoukankan      html  css  js  c++  java
  • C#自动化IO/XML作业

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

                        }

                    }

                }

            }

        }

    }

  • 相关阅读:
    Hibernate+mysql 中文问题解决方案.
    FpSpread表格控件,FpSpread事件介绍(一)
    如何实现打开有宏的EXCEL时不提示
    使用VB.Net写一个简单的数据访问层(不能称ORM):CRUD操作
    Asp.NET 时间Since转换
    64位操作系统上。NET操作MSMQ的问题
    IIS7配置管理Windows2008 64位系统IIS7的问题
    数据库开发批量附加数据库
    IIS7中对静态文件的处理
    techsailor三步曲
  • 原文地址:https://www.cnblogs.com/LanTianYou/p/4225657.html
Copyright © 2011-2022 走看看