一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

C# list 新用法

 跳跳圖書館502 2012-09-12

用list的方法來完成在一個(gè)數(shù)型結(jié)構(gòu)的文件夾中,可能在拷貝時(shí)中間出現(xiàn)了差錯(cuò),希望有一段代碼來比較兩個(gè)文件夾中的異同數(shù)目有多少.

主要用到的方法是:

1:

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

返回當(dāng)前目錄的文件列表。

2:

List.Intersect(List);

找出兩陣列相同的項(xiàng)目.

3:

List.Except(List);

找出兩陣列不相同的項(xiàng)目-Except

新建一個(gè)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();
        }
    }

圖片如下:

新建一個(gè)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);
          }
      }
  }

界面如下:

新加一個(gè)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");
           }
 
       }

界面如下:

歡迎拍磚.

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    人妻少妇av中文字幕乱码高清| 99少妇偷拍视频在线| 日本女优一区二区三区免费| 久久热在线视频免费观看| 91久久国产福利自产拍| 精品欧美在线观看国产| 亚洲日本加勒比在线播放 | 女人精品内射国产99| 日韩精品区欧美在线一区| 黄片在线免费观看全集| 日本在线视频播放91| 欧美日韩精品久久第一页| 日本人妻的诱惑在线观看| 自拍偷拍福利视频在线观看| 久草视频在线视频在线观看| 欧美整片精品日韩综合| 人妻内射在线二区一区| 91亚洲国产—区=区a| 日韩免费国产91在线| 又色又爽又黄的三级视频| 亚洲香艳网久久五月婷婷| 日本和亚洲的香蕉视频| 亚洲日本韩国一区二区三区| 久久精品国产99精品亚洲| 色婷婷日本视频在线观看| 日韩欧美第一页在线观看| 成人精品视频在线观看不卡| 99久久人妻精品免费一区| 国产日韩欧美在线播放| 熟妇久久人妻中文字幕| 精品一区二区三区不卡少妇av| 精品香蕉国产一区二区三区| 亚洲一区二区三区熟女少妇| 国产又粗又猛又大爽又黄| 日本淫片一区二区三区| 日韩黄色大片免费在线| 亚洲综合香蕉在线视频| 久久99热成人网不卡| 日本深夜福利在线播放| 初尝人妻少妇中文字幕在线| 黑色丝袜脚足国产一区二区|