c# - Compare folders using LINQ -
edit: editing question more details:
i'm working on comparing 2 huge folders , figuring out files common in both folders. msdn has program using linq solve : article msdn
however there problem i'm trying fix.
let's have 2 folders. foldera , folderb. foldera , folder b has 2 subfolders 1 , 2.
- c:\foldera\1\a.aspx
- c:\foldera\2\b.aspx
- c:\folderb\1\a.aspx
- c:\folderb\1\b.aspx
a.aspx , b.aspx identical in both foldera , folderb. note b.aspx exist in different subfolders though.
current result: c:\foldera\1\a.aspx c:\foldera\2\b.aspx
i expect result matches c:\foldera\1\a.aspx because match folder structure , file identical.
would able modify filecompare class perform comparison of files lies in same directory structure?
or
what changes should make make sure comparison done correctly.
thanks! sanjeev
to make work, need adjust how equals()
function operates. suggestion follows:
step 1 - make path
variables available equals()
method:
class comparedirs { static void main(string[] args) { // create 2 identical or different temporary folders // on local drive , change these file paths. string patha = @"c:\testdir"; string pathb = @"c:\testdir2"; ...
becomes
class comparedirs { private string patha, pathb; static void main(string[] args) { // create 2 identical or different temporary folders // on local drive , change these file paths. patha = @"c:\testdir"; pathb = @"c:\testdir2"; ...
step 2 - change equals()
method consider information:
i suggest using .replace(patha, pathb)
enable directories of files compared if path same. files in same subdirectory structure have same directory overall (after replace operation has been performed).
public bool equals(system.io.fileinfo f1, system.io.fileinfo f2) { return (f1.name == f2.name && f1.length == f2.length && f1.directoryname.replace(patha, pathb) == f2.directoryname.replace(patha, pathb) ); }
Comments
Post a Comment