C# list 新用法
用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<string> errorList) { InitializeComponent(); if (errorList != null) { foreach (string str in errorList) lstError.AppendText(str); } } public ErrorFrom() { InitializeComponent(); } }
图片如下:
新建一个MessageFrom.cs From,代码如下:
public partial class MessageFrom : Form { public MessageFrom() { InitializeComponent(); } public MessageFrom(List<string> MessageList) { InitializeComponent(); if (MessageList != null) { foreach (string str in MessageList) lstMessage.AppendText(str); } } }
界面如下:
新加一个CompareFolder.cs From,代码如下:
下面正式看代码:
public partial class CompareFolder : Form { public CompareFolder() { InitializeComponent(); txtFolderOne.ReadOnly = true; txtFolderTwo.ReadOnly = true; } /// <summary> /// /// </summary> /// <param name="errorList"></param> private void HandleError(List<string> errorList) { if (errorList != null && errorList.Count > 0) { ErrorFrom errorForm = new ErrorFrom(errorList); errorForm.Show(); } } /// <summary> /// /// </summary> /// <param name="messageList"></param> private void HandleMessage(List<string> messageList) { if (messageList != null && messageList.Count > 0) { MessageFrom messageForm = new MessageFrom(messageList); messageForm.Show(); } } public List<string> ErrorList = new List<string>(); public List<string> MessageList = new List<string>(); 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<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories); IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories); List<string> filelistone = new List<string>(); List<string> filelisttwo = new List<string>(); //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"); } }
界面如下:
欢迎拍砖.