C# 比较两个文件夹中文件的异同

C# 比较两个文件夹中文件的异同

 

     这几天要写个工具,来比较两个文件夹下的N个xml文件内节点属性值的异同,起初两个文件夹内的xml文件很工整,名字一样,数量也一样,直接依次比较就可以。后来文件夹内的文件有时候数量不一样,这样的话必须过滤掉不同的,修改代码的话,感觉还是在获取文件目录的时候提前修改,这样一劳永逸,后边的代码就完全不用修改了。 查了一些资料,发现几段给力的代码,稍作修改,顺利完工。

      主要资料来源 : spring yang博客: http://www.cnblogs.com/springyangwc/archive/2011/03/18/1988177.html

                            MSDN: http://msdn.microsoft.com/zh-cn/library/bb546137(v=vs.90).aspx 

 

      核心代码如下:

                    //获得两个文件夹目录
DirectoryInfo di1 = new DirectoryInfo(txtBoxOldPath.Text);
DirectoryInfo di2 = new DirectoryInfo(txtBoxNewPath.Text);
//获得文件夹下所有文件信息(不包含子目录)
FileSystemInfo[] dsi1 = di1.GetFileSystemInfos("*.xml");
FileSystemInfo[] dsi2 = di2.GetFileSystemInfos("*.xml");

//find All Files. (包含子目录)
IEnumerable<System.IO.FileInfo> list1 = di1.GetFiles("*.xml", System.IO.SearchOption.AllDirectories);
IEnumerable<System.IO.FileInfo> list2 = di2.GetFiles("*.xml", System.IO.SearchOption.AllDirectories);

List<string> filelistone = new List<string>();
List<string> filelisttwo = new List<string>();
List<string> errorlist = new List<string>();
List<string> messagelist = new List<string>();
//To List with Relative Path. 把文件相对路径添加到list
foreach (System.IO.FileInfo files in list1)
{
string filepathone = files.DirectoryName.Replace(txtBoxOldPath.Text, "") + "\\" + files.Name;
filelistone.Add(filepathone);
}

//To List with Relative Path.
foreach (System.IO.FileInfo files in list2)
{
string filepathtwo = files.DirectoryName.Replace(txtBoxNewPath.Text, "") + "\\" + files.Name;
filelisttwo.Add(filepathtwo);
}

//check Is Same 两个文件夹下文件数量、名字完全相同,把文件列表放到listbox 控件里
bool areIdentical = filelistone.SequenceEqual(filelisttwo);
if (areIdentical == true)
{
messagelist.Add("the two folders are the same.\n");
lstBoxOldScript.Visible = true;
//filelistone.Insert(0,"the two folders are the same.\n");
lstBoxOldScript.DataSource = filelistone;
lstBoxNewScript.Visible = true;
//filelisttwo.Insert(0,"the two folders are the same.\n");
lstBoxNewScript.DataSource = filelisttwo;
}
else
{
errorlist.Add("The two folders are not the same.\n");



//Find Same 如果两个文件夹不完全相同,找出相同的文件,添加到messagelist
var queryCommonFiles = filelistone.Intersect(filelisttwo);
if (queryCommonFiles.Count() > 0)
{
//messagelist.Add("The following files are in both folders:\n"); 可以加入Log
foreach (var v in queryCommonFiles)
{
messagelist.Add(v);
}
}
else
{
errorlist.Add("There are no common files in the two folders.\n");
}

// Find not Same. 查找不同的文件,添加到列表里面errorlist
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");
}

//按需求执行操作,把列表里面的值添加到Winform ListBox控件中
if (dsi1.Length < 1)
{
MessageBox.Show("The folder is empty!", "error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
lstBoxOldScript.Visible = true;
lstBoxOldScript.DataSource = messagelist;
}
if (dsi2.Length < 1)
{
MessageBox.Show("The folder is empty!", "error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
lstBoxNewScript.Visible = true;
lstBoxNewScript.DataSource = messagelist;
}
}
}



posted on 2011-09-23 15:31  远远有多远  阅读(2444)  评论(1编辑  收藏  举报

导航