我开发了一个小软件,它允许我比较 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:对不起我的中等英语