首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将两个目录与文件进行比较,然后在另一个文件中给出结果差异。C#

将两个目录与文件进行比较,然后在另一个文件中给出结果差异。C#
EN

Stack Overflow用户
提问于 2016-12-05 21:34:23
回答 2查看 1.8K关注 0票数 0

我正在比较两个目录中的文件。一个是旧目录( old ),另一个是包含更多文件的新目录( new )。我能够打印显示所有添加的文件的输出,但它只显示结果文件中的一个文件,我实际上希望它在其中运行。如何让它显示结果文件中添加的所有文件?

Visual Studio上的输出

结果文件中的输出

这是我的代码:

代码语言:javascript
复制
public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{      
    String New = @"C:\Compare\New";
    String Old = @"C:\Compare\Old";
    String Result = @"C:\Compare\Result";
    DirectoryInfo dir1 = new DirectoryInfo(New);
    DirectoryInfo dir2 = new DirectoryInfo(Old);

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

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

    // Check if same or not.
    // bool because of FileCompare
    bool areSame = list1.SequenceEqual(list2, compare);

    if (areSame == true)
    {
        MessageBox.Show("The Directories match.");
    }
    else
    {
        MessageBox.Show("Directories do not the same.. Please Review 'Result.text'.");
    }

    // Find the set difference between the two folders.  
    // Print to the result.txt
    var difFromOld = (from file in list1
            select file).Except(list2, compare);

        foreach (var result in difFromIOld)
        {
            StreamWriter file = new StreamWriter(Result);
            file.WriteLine("The following files do not match between Directories:");
            file.WriteLine("");
            file.WriteLine(result.Name + "\n", true);
            file.Close();
            Console.WriteLine(result.Name);
        }
}
}


class FileCompare : IEqualityComparer<FileInfo>
{
    public FileCompare() { }

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

    public int GetHashCode(FileInfo fi)
    {
        string s = String.Format("{0}{1}", fi.Name, fi.Length);
        return s.GetHashCode();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-05 21:42:28

您可以在每次迭代中通过foreach循环创建一个新的StreamWriter。默认情况下,StreamWriter覆盖文件内容。

最好的解决方案是改变您的循环,这样看起来就像

代码语言:javascript
复制
using(var file = new StreamWriter(Result)) {
  foreach (var result in difFromIOld)
  {
  /* Write to 'file' here */
  }
}

您还可以指定StreamWriter应该追加,而不是替换文件:new StreamWriter(Result, append: true)。但最好只打开文件一次。

票数 6
EN

Stack Overflow用户

发布于 2016-12-05 22:05:19

您可以试试这个,我没有证明代码您可能需要修改一些东西。

代码语言:javascript
复制
IEnumerable<FileInfo> list1 = dir1.GetFiles("*.*", SearchOption.AllDirectories);
IEnumerable<FileInfo> list2 = dir2.GetFiles("*.*", SearchOption.AllDirectories);
IEnumerable<FileInfo> DifferentFiles;
//if neww dir has more files than old dir
int j;
foreach ( var dir in list1 )
{
    j=1;
    for (int i=0; i<list2.count; i++) 
    {   
        if (!compare.Equals(dir1,list2[i])) j++
    }
    if (j==list2.count){
        //dir1 is not in list2
        DifferentFiles.Add(dir1);
    }
}
/*
//Add this code to know if there are directories in List2  that are not in list1
//ass the first for is not proving it. 
foreach ( var dir2 in list2 )
{
    j=1;
    for (int i=0; i<list1.count; i++) 
    {   
        if (!compare.Equals(dir2,list1[i])) j++
    }
    if (j==list1.count){
        //dir2 is not in list1
        if(!DifferentFiles.Contains(dir2)) DifferentFiles.Add(dir2);    
    }
}
*/

StreamWriter file = new StreamWriter(Result);
//if 2 for is not added
file.WriteLine("The following files in the new Directory are not in the Old one:");
//if 2 for is added
    //file.WriteLine("The following files are not in both directories");
file.WriteLine("");
foreach(var result in DifferentFiles)
{
    file.WriteLine(result.Name + "\n", true);
        Console.WriteLine(result.Name);
}
file.Close();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40983761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档