2

我开发了一个小软件,它允许我比较 2 个文件夹并添加文件。我主要从 msdn 获取代码。

这看起来可以很好地处理某些文件,但我在处理 2 个文件夹(292 和 268 个文件)时遇到了一些问题。

在我使用 2 个大文件夹的情况下,我通过该方法发现了这些差异:

C:\Users\Puppie\Desktop\KittiesWarWP8\KittiesWarWP8.sdf
C:\Users\Puppie\Desktop\KittiesWarWP8\Debug\KWGameComp\KWGameComp.pdb
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\Bin\x86\Debug\KWGame_Debug_x86.xap
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\KWGame.csprojResolveAssemblyReference.cache
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\XapCacheFile.xml
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\fxc.write.1.tlog
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\KWGameComp.write.1.tlog
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\link.write.1.tlog
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Thumbs.db
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\Thumbs.db
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\obj\x86\Debug\KWGamePages.csprojResolveAssemblyReference.cache

但我看到错过了一些文件 292 - 268 = 24 个文件。该方法发现我只有11 .... 我检查并找到了一个看起来在方法中被忽略的小文件夹:

C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\AlignmentGrid.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\ApplicationIcon.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileLarge.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileMedium.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileSmall.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileMediumLarge.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileSmall.png

这些文件完全被忽略了。我制作了 2 个文件夹来测试这些文件(文件夹 1 和 2),并且通过相同方法的差异结果是:

C:\Users\Puppie\Desktop\1\Assets\AlignmentGrid.png
C:\Users\Puppie\Desktop\1\Assets\ApplicationIcon.png
C:\Users\Puppie\Desktop\1\Assets\Tiles\FlipCycleTileLarge.png
C:\Users\Puppie\Desktop\1\Assets\Tiles\FlipCycleTileMedium.png
C:\Users\Puppie\Desktop\1\Assets\Tiles\FlipCycleTileSmall.png
C:\Users\Puppie\Desktop\1\Assets\Tiles\IconicTileMediumLarge.png
C:\Users\Puppie\Desktop\1\Assets\Tiles\IconicTileSmall.png

Tada 当文件夹很小时,他完美地发现了差异,在测试 1 的目标文件夹和测试 2 的目标文件夹中,这些图片不存在(我已经检查过)。

所以我只想知道msdn上的方法是否有限制?

有真正的源代码:http: //msdn.microsoft.com/en-us/library/bb546137.aspx

我的只是稍作修改(添加目录):

比较方法:

路径 A:C:\Users\Puppie\Desktop\KittiesWarWP8\

路径 B:C:\Users\Puppie\Desktop\Logiciel pour projet\KittiesWarWP8\

 private void checkFiles()
    {
        List<string> dir = new List<string>();
        int lenghtPath = pathA.Length - (new DirectoryInfo(pathA).Name).Length - 1;

        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
        System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);

        // Take a snapshot of the file system.
        IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*", System.IO.SearchOption.AllDirectories);
        IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*", System.IO.SearchOption.AllDirectories);

        //A custom file comparer defined below
        FileCompare myFileCompare = new FileCompare();

        // Find the set difference between the two folders. 
        // For this example we only check one way. 
        var queryList1Only = (from file in list1 select file).Except(list2, myFileCompare);
        int temp;

        foreach (var v in queryList1Only)
        {
            if (dir.Count == 0)
            {

                if (!new FileInfo(pathB + (v.FullName.Remove(0, pathA.Length))).Exists)
                {
                    //we do a little verification
                    //the software must do not take the base folder
                    if (!(new FileInfo(v.FullName).DirectoryName + @"\").Equals(pathA))
                    {
                        dir.Add(new FileInfo(v.FullName).DirectoryName);
                        this.checkedListBox1.Items.Add("INEXISTANT |    " + dir[dir.Count - 1]);
                    }

                    this.checkedListBox1.Items.Add("INEXISTANT |    " + v.FullName);
                }
                else
                {
                    //we do a little verification
                    //the software must do not take the base folder
                    if (!(new FileInfo(v.FullName).DirectoryName + @"\").Equals(pathA))
                    {
                        dir.Add(new FileInfo(v.FullName).DirectoryName);
                        this.checkedListBox1.Items.Add("MODIFIE        |    " + dir[dir.Count - 1]);
                    }

                    this.checkedListBox1.Items.Add("MODIFIE        |    " + v.FullName);
                }
            }
            else
            {
                bool find = false;
                int save = 0;

                for (int i = 0; i < dir.Count; i++)
                {
                    if ((new FileInfo(v.FullName).DirectoryName).Equals(dir[i]))
                    {
                        find = true;
                        save = i;
                        i = dir.Count;
                    }
                }

                if (!find)
                {
                    if (!new FileInfo(pathB + (v.FullName.Remove(0, pathA.Length))).Exists)
                    {
                        dir.Add(new FileInfo(v.FullName).DirectoryName);
                        this.checkedListBox1.Items.Add("INEXISTANT |    " + dir[dir.Count - 1]);

                        this.checkedListBox1.Items.Add("INEXISTANT |    " + v.FullName);
                    }
                    else
                    {
                        dir.Add(new FileInfo(v.FullName).DirectoryName);
                        this.checkedListBox1.Items.Add("MODIFIE        |    " + dir[dir.Count - 1]);

                        this.checkedListBox1.Items.Add("MODIFIE        |    " + v.FullName);
                    }
                }
                else
                {
                    if (!new FileInfo(pathB + (v.FullName.Remove(0, pathA.Length))).Exists)
                        this.checkedListBox1.Items.Add("INEXISTANT |    " + v.FullName);
                    else
                        this.checkedListBox1.Items.Add("MODIFIE        |    " + v.FullName);
                }

            }

        }
    }

比较类:

class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
{
    public FileCompare() { }

    public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
    {
        return (f1.Name == f2.Name &&
                f1.Length == f2.Length);
    }

    // Return a hash that reflects the comparison criteria. According to the  
    // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must 
    // also be equal. Because equality as defined here is a simple value equality, not 
    // reference identity, it is possible that two or more objects will produce the same 
    // hash code. 
    public int GetHashCode(System.IO.FileInfo fi)
    {
        string s = String.Format("{0}{1}", fi.Name, fi.Length);
        return s.GetHashCode();
    }
}

在此先感谢您的帮助 ;)。

PS:对不起我的中等英语

4

2 回答 2

0

好的,我在比较文件中进行了如下更改:

public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
        {
            return ((f1.FullName.Remove(0, this.pathA.Length)).Equals(f2.FullName.Remove(0, this.pathB.Length)) &&
                    f1.Length == f2.Length);
        }

现在结果改变了很多^^:

C:\Users\Puppie\Desktop\KittiesWarWP8\KittiesWarWP8.sdf C:\Users\Puppie\Desktop\KittiesWarWP8\Debug\KWGameComp\KWGameComp.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\Debug\test3Comp\SimpleVertexShader.cso C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\Bin\Debug\ApplicationIcon.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\Bin\Debug\Background.png C:\Users\Puppie \Desktop\KittiesWarWP8\KWGame\KWGame\Bin\x86\Debug\KWGameComp.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\Bin\x86\Debug\KWGame_Debug_x86.xap C:\Users\Puppie\Desktop \KittiesWarWP8\KWGame\KWGame\obj\Debug\App.g.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\Debug\App.gics C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame \KWGame\obj\Debug\KWGame.dll C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\Debug\KWGame.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache C:\Users\Puppie \Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\KWGame.csprojResolveAssemblyReference.cache C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\MainPage.gics C:\Users\Puppie \Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\XapCacheFile.xml C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\fxc.write.1.tlog C:\Users\Puppie\Desktop \KittiesWarWP8\KWGame\KWGameComp\Debug\KWGameComp.write.1.tlog C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\link.write.1.tlog C:\Users\Puppie\Desktop\KittiesWarWP8 \KWGame\KWGameLib\bin\Windows Phone\Debug\KWGameLib.dll C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameLib\bin\Windows Phone\Debug\KWGameLib.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameLib\obj\Windows Phone\Debug\KWGameLib.dll C: \Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameLib\obj\Windows Phone\Debug\KWGameLib.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\AlignmentGrid.png C:\Users\Puppie\Desktop\ KittiesWarWP8\KWGamePages\Assets\ApplicationIcon.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Thumbs.db C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileLarge.png C:\ Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileMedium.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileSmall.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileMediumLarge.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileSmall.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages \Assets\Tiles\Thumbs.db C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Bin\x86\Debug\KWGameComp.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481 -4323-8D20-8F5ADCB23D92.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\obj \Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\obj\x86\Debug\KWGamePages.csprojResolveAssemblyReference.cache C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Properties\AppManifest.xml C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGenerator\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs C:\Users\Puppie \Desktop\KittiesWarWP8\KWMapGenerator\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGenerator\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B010-B56E-8E C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGeneratorNokia\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGeneratorNokia\obj\Debug\TemporaryGeneratedFile_5937a670-0e -877b-f7221da3dda1.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGeneratorNokia\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

43 个差异 ^^'(292 - 269 = 23(不存在的文件)+ 20(修改的文件))。正如我们所看到的:

    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\AlignmentGrid.png
    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\ApplicationIcon.png
    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileLarge.png
    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileMedium.png
    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileSmall.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileMediumLarge.png
    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileSmall.png

实际上问题来自误报^^。我认为我的 copare 方法很好,但如果您再次看到错误,请告诉我在哪里。我会检查我是否有 20 个实际修改过的文件^^。

如果一切顺利,我会批准@Joel Rondeau 的回答;)。

于 2013-11-09T13:00:22.950 回答
0

我发现你的FileCompare课有问题。在检查相等性时,您只是在比较文件Name。您还在检查大小,这将有所帮助,但不能保证不同目录中同名文件的误报。

最好检查 和 的相对dir1路径dir2。如果没有看到完整的文件列表和大小,我不能说这会解决您的问题,但它是一个可能导致问题的地方。

更新:更多解释。

当我看到有 24 个不同的文件,但代码只出现了 11 个时,我的第一个想法是代码可能发现了误报——其中一个目录结构中的单个文件匹配了 2 个或更多文件另一个目录结构。例如:

目录 1 文件:
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\AlignmentGrid.png

dir2 文件:
C:\Users\Puppie\Desktop\Logicielpourprojet\KittiesWarWP8\KWGamePages\Assets\AlignmentGrid.png

C:\Users\Puppie\Desktop\Logicielpourprojet\KittiesWarWP8\KWGamePages\AdditionalAssets\AlignmentGrid.png

因此,如果您在不同的多个目录中有重复的文件,它们不会显示使用您的FileCompare,因为您FileCompare认为它们是同一个文件。

使用我的示例,我希望该dir1文件与第一个dir2文件匹配,但与第二个文件不匹配。因为相对路径不同,我认为它们是不同的文件。如果您还认为它们是不同的文件,那么您需要更新您的类以通过检查相对于和FileCompare的路径来区分两者。dir1dir2

于 2013-11-08T18:19:54.580 回答