English 中文(简体)
快速文档与Linq比较
原标题:Fast FileSize Compare with Linq

i 设有两个档案目录,希望确保两者都相同。 因此,i ve制造了一个把所有档案放在档案信息阵列上的问话。 我把所有档案都归案,现在要比较每个团体的档案,供他们最后审定。

But, to be honest, like i do this, its far to slow. Any Idea how i could compare the Files within a Group over Linq about their Length and let me do sth if the are different?

...

FileInfo[] fiArrOri5 = d5ori.GetFiles("*.*", System.IO.SearchOption.TopDirectoryOnly);
FileInfo[] fiArrNew5 = d5new.GetFiles("*.*", System.IO.SearchOption.TopDirectoryOnly);

FileInfo[] AllResults = new FileInfo[fiArrNew5.Length+fiArrOri5.Length];
fiArrNew5.CopyTo(AllResults, 0);
fiArrOri5.CopyTo(AllResults, fiArrNew5.Length);

var duplicateGroups = AllResults.GroupBy(file => file.Name);

        foreach (var group in duplicateGroups)
        {
            AnzahlElemente = group.Count();

            if (AnzahlElemente == 2)
            {
                if (group.ElementAt(0).Length != group.ElementAt(1).Length)
                {
                    // do sth
                }
            }

            ...
        }

EDIT:

如果只操作以下刀,则超速(~00:00:0005156)

Console.WriteLine(group.ElementAt(0).LastWriteTime);

if i run only the following snippet, it runs super slow. (~00:00:00:0750000)

Console.WriteLine(group.ElementAt(1).LastWriteTime);

任何想法为什么?

问题回答

I m not sure this will be faster - but this is how I would have done this:

var folderPathOne = "FolderPath1";
var folderPathTwo = "FolderPath2";

//Get all the filenames from dir 1
var directoryOne = Directory
    .EnumerateFiles(folderPathOne, "*.*", SearchOption.TopDirectoryOnly)
    .Select(Path.GetFileName);

//Get all the filenames from dir 2
var directoryTwo = Directory
    .EnumerateFiles(folderPathTwo, "*.*", SearchOption.TopDirectoryOnly)
    .Select(Path.GetFileName);

//Get only the files that appear in both directories
var filesToCheck = directoryOne.Intersect(directoryTwo);

var differentFiles = filesToCheck.Where(f => new FileInfo(folderPathOne + f).Length != new FileInfo(folderPathTwo + f).Length);

foreach(var differentFile in differentFiles)
{
    //Do something
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签