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作业的更多相关文章

  1. IO流作业

    IO流作业 一.    填空题 Java IO流可以分为    字节流          和处理流两大类,其中前者处于IO操作的第一线,所有操作必须通过他们进行. 输入流的唯一目的是提供通往数据的通道 ...

  2. 2017.12.20 Java中的 IO/XML学习总结 File类详细

    IO / XML 一.File类 1.定义/概念 Java是面向对象的语言,要想把数据存到文件中,就必须要有一个对象表示这个文件.File类的作用就是代表一个特定的文件或目录,并提供了若干方法对这些文 ...

  3. python接口自动化-发xml格式post请求

    前言 post请求相对于get请求多一个body部分,body部分常见的数据类型有以下四种(注意是常见的,并不是只有4种) application/x-www-form-urlencoded appl ...

  4. javaee IO流作业02

    package Zy; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.Fil ...

  5. javaee IO流作业

    package Zy; import java.io.Serializable; public class Student implements Serializable{ private stati ...

  6. 老男孩python自动化运维作业2

    拿到要求真不知道怎么写,不能还要写个商城页面吧: 最后还是用了input()模拟用户操作吧- -!不就操作个字典吗(字典模拟商品数据). python版本: >>>import sy ...

  7. 老男孩python自动化运维作业1

    #!/usr/bin/env pthon #字典操作三级菜单 “b”返回上一级菜单,“q”退出. menu={"BJ":{"cp":{1:1,2:2,3:3}, ...

  8. 简单Java类与XML之间的转换

    需要的jar包:xmlpull_1_0_5.jar,xstream-1.4.1.jar) 1.工具类XstreamUtil package com.learn.util; import com.tho ...

  9. JAVA对象和XML文档、原来他们之间还有这一出

    最近项目开发中遇到一个问题,访问接口不再通过url地址请求的方式,而是 通过socket发送xml格式的报文到指定服务器来进行信息的统一认证.. 因此组装xml格式的报文字符串以及解析服务器返回的xm ...

随机推荐

  1. codeforces-Glass Carving(527C)std::set用法

    C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. [转]史上最全的MSSQL复习笔记

    阅读目录 1.什么是SQL语句 2.使用sql语句创建数据库和表 3.创建数据表 4.数据完整性约束 5.四中基本字符类型说明 6.SQL基本语句 7.类型转换函数 8.日期函数 9.数学函数 10. ...

  3. 3D语音天气球(源码分享)——在Unity中使用Android语音服务

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...

  4. 类名.class, class.forName(), getClass()区别

    1:Class cl=A.class; JVM将使用类A的类装载器, 将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作.返回类A的Class的对象. 2:Class cl=对象引 ...

  5. location.reload

    location.reload()-----重新加载缓存页面 location.reload(true)------重新加载服务器页面

  6. js中把JSON字符串转换成JSON对象最好的方法

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...

  7. ubuntu下配置SVN服务器

    自己买的阿里云服务器.可是我老感觉没有SVN上传代码下载代码太不方便!决定配置个SVN服务器! 1.安装Subversion $ sudo apt-get install subversion $ s ...

  8. Reporting Service报表项默认可见+号和-号的显示问题

    在Reporting Service里面可以设置报表项(组.tablix行.tablix列.文本框等所有SSRS报表项)的可见性,并且可以设置某个报表项的可见性由点击另外一个报表项来控制,比如报表项A ...

  9. 视频处理控件TVideoGrabber如何对屏幕进行录制/压缩

    TVideoGrabber可以对屏幕进行录制和压缩,本文来详细的说明在多种情况下TVideoGrabber是如何实现屏幕的录制和压缩. 屏幕录制 当VideoSource = vs_ScreenRec ...

  10. gerrit-git

    解释为什么gerrit中的push是需要用refs/for/master http://stackoverflow.com/questions/10461214/why-is-git-push-ger ...