博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# list 新用法
阅读量:6831 次
发布时间:2019-06-26

本文共 4773 字,大约阅读时间需要 15 分钟。

用list的方法来完成在一个数型结构的文件夹中,可能在拷贝时中间出现了差错,希望有一段代码来比较两个文件夹中的异同数目有多少.

主要用到的方法是:

1:

System.IO.DirectoryInfo.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

返回当前目录的文件列表。

2:

List.Intersect(List);

找出兩陣列相同的項目.

3:

List.Except(List);

找出兩陣列不相同的項目-Except

新建一个ErrorFrom.cs  From

代码如下:

public partial class ErrorFrom : Form    {        public ErrorFrom(List
errorList) { InitializeComponent(); if (errorList != null) { foreach (string str in errorList) lstError.AppendText(str); } } public ErrorFrom() { InitializeComponent(); } }

图片如下:

2011031816053693.jpg

新建一个MessageFrom.cs  From,代码如下:

public partial class MessageFrom : Form    {        public MessageFrom()        {            InitializeComponent();        }        public MessageFrom(List
MessageList) { InitializeComponent(); if (MessageList != null) { foreach (string str in MessageList) lstMessage.AppendText(str); } } }

界面如下:

2011031816060276.jpg

新加一个CompareFolder.cs From,代码如下:

下面正式看代码:

public partial class CompareFolder : Form    {        public CompareFolder()        {            InitializeComponent();            txtFolderOne.ReadOnly = true;            txtFolderTwo.ReadOnly = true;        }        ///         ///         ///         ///         private void HandleError(List
errorList) { if (errorList != null && errorList.Count > 0) { ErrorFrom errorForm = new ErrorFrom(errorList); errorForm.Show(); } } ///
/// /// ///
private void HandleMessage(List
messageList) { if (messageList != null && messageList.Count > 0) { MessageFrom messageForm = new MessageFrom(messageList); messageForm.Show(); } } public List
ErrorList = new List
(); public List
MessageList = new List
(); private void btnFolderOne_Click(object sender, EventArgs e) { folderBrowserDialog1.ShowDialog(); txtFolderOne.Text = folderBrowserDialog1.SelectedPath; } private void btnFolderTwo_Click(object sender, EventArgs e) { folderBrowserDialog1.ShowDialog(); txtFolderTwo.Text = folderBrowserDialog1.SelectedPath; } private void btnRun_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtFolderOne.Text)) { ErrorList.Add("Please Select folder one."); HandleError(ErrorList); return; } if (string.IsNullOrEmpty(txtFolderTwo.Text)) { ErrorList.Add("Please Select folder two."); HandleError(ErrorList); return; } CompareSubDirectory(txtFolderOne.Text, txtFolderTwo.Text); HandleMessage(MessageList); HandleError(ErrorList); } private void CompareSubDirectory(string urlone, string urltwo) { string pathA =urlone; string pathB = urltwo; System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA); System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB); //find All Files. IEnumerable
list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories); IEnumerable
list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories); List
filelistone = new List
(); List
filelisttwo = new List
(); //To List with Relative Path. foreach (System.IO.FileInfo files in list1) { string filepathone = files.DirectoryName.Replace(pathA, "") + "\\" + files.Name; filelistone.Add(filepathone); } //To List with Relative Path. foreach (System.IO.FileInfo files in list2) { string filepathtwo = files.DirectoryName.Replace(pathB, "") + "\\" + files.Name; filelisttwo.Add(filepathtwo); } //check Is Same bool areIdentical = filelistone.SequenceEqual(filelisttwo); if (areIdentical == true) { MessageList.Add("the two folders are the same.\n"); } else { ErrorList.Add("The two folders are not the same.\n"); } //Find Same var queryCommonFiles = filelistone.Intersect(filelisttwo); if (queryCommonFiles.Count() > 0) { MessageList.Add("The following files are in both folders:\n"); foreach (var v in queryCommonFiles) { MessageList.Add(v + "\n"); } } else { ErrorList.Add("There are no common files in the two folders.\n"); } // Find not Same. var queryList1Only = (from file in filelistone select file).Except(filelisttwo); ErrorList.Add("The following files are in list1 but not list2:\n"); foreach (var v in queryList1Only) { ErrorList.Add(v + "\n"); } var querylist2Only = (from file in filelisttwo select file).Except(filelistone); ErrorList.Add("The following files are in list2 but not list1:\n"); foreach (var v in querylist2Only) { ErrorList.Add(v+"\n"); } }

界面如下:

2011031816062455.jpg

欢迎拍砖.

转载地址:http://gejkl.baihongyu.com/

你可能感兴趣的文章
面向对象程序设计进阶(二)
查看>>
通用输入输出端口 - GPIO
查看>>
JSP内置对象和EL内置对象
查看>>
Python开发【第十九篇】:Python操作MySQL
查看>>
oracle单词
查看>>
从头开始db-oracle
查看>>
Python3学习笔记25-logging模块
查看>>
RHEL6.5 LVM使用解析
查看>>
Windows 8 应用商店正式面向全部开发者开放
查看>>
lamp系列-MySQL主从复制原理视频(老男孩出品)
查看>>
如何撰写优秀系统运维架构方案及推动实施案例分享
查看>>
Centos5.6 x86_64下安装DRBD+Heartbeat+NFS
查看>>
Cocos2d-x Eclipse下程序运行产生错误Effect initCheck() returned -1
查看>>
Lync Server 2010的部署系列(四) outlook无法加入联机会议
查看>>
Windows Server 2012安装SQL 2012
查看>>
MS UC 2013-0-虚拟机-标准化-部署-2-模板机-制作-5
查看>>
准备重新回归信息安全产业
查看>>
向DBA致敬~
查看>>
RHEL6基础二十七之quota磁盘配额管理
查看>>
Bundle 小镇中由 EasyUI 引发的“血案”
查看>>