コード例 #1
0
        private void TryCompare()
        {
            if (file1 == null || file2 == null)
            {
                return;
            }

            listview_Main.Items.Clear();

            List <ListViewItem> results = new List <ListViewItem>();

            Dictionary <String, FileData> allFiles = new Dictionary <string, FileData>();

            foreach (KeyValuePair <String, FileData> entry in file1.Files)
            {
                allFiles.Add(entry.Key, entry.Value);
            }

            foreach (KeyValuePair <String, FileData> entry in file2.Files)
            {
                if (!allFiles.ContainsKey(entry.Key))
                {
                    allFiles.Add(entry.Key, entry.Value);
                }
            }

            foreach (KeyValuePair <String, FileData> entry in allFiles)
            {
                String[]     filenameParts = entry.Value.filename.Split(Path.DirectorySeparatorChar);
                ListViewItem item          = new ListViewItem(entry.Key);

                item.SubItems.Add(String.Format(new FileSizeFormatProvider(), "{0:fs}", entry.Value.size));
                item.SubItems.Add(entry.Key.Substring((entry.Key.Length - 3)));
                item.SubItems.Add(filenameParts[filenameParts.Length - 1]);
                item.SubItems[1].Tag = entry.Value.size;

                if (!file2.Files.ContainsKey(entry.Key))                   //if new file does not contain the entry
                // removed file
                {
                    item.ImageKey = "removed";
                    if (checkbox_Removed.Checked)
                    {
                        results.Add(item);
                    }
                    continue;
                }

                if (!file1.Files.ContainsKey(entry.Key))                   // if old file does not contain the entry
                // added file
                {
                    item.ImageKey = "added";
                    if (checkbox_Added.Checked)
                    {
                        results.Add(item);
                    }
                    continue;
                }

                byte[] fileData1 = file1.GetFile(entry.Key);
                byte[] fileData2 = file2.GetFile(entry.Key);

                bool sameFile = (fileData1.Length == fileData2.Length);

                if (sameFile)
                {
                    for (int i = 0; i < fileData1.Length; i++)
                    {
                        sameFile = (fileData1[i] == fileData2[i]);
                        if (!sameFile)
                        {
                            break;
                        }
                    }
                }

                if (!file1.Files[entry.Key].Equals(file2.Files[entry.Key]) || !sameFile)
                {
                    // changed file
                    item.ImageKey = "changed";
                    if (checkbox_Changed.Checked)
                    {
                        results.Add(item);
                    }
                    continue;
                }

                // unchanged file
                item.ImageKey = "same";
                if (checkbox_Unchanged.Checked)
                {
                    results.Add(item);
                }
            }

            if (results.Count > 0)
            {
                listview_Main.Items.AddRange(results.ToArray());
            }
        }