fileinfo - How do I compare one collection of files to another in c#? -


I'm just learning C # (it's been weakened for about 2 days now) and I've decided That, for purposes of tilt, I will recreate an old app in VB6 (typically on a network) to synchronize files.

When I wrote the code in VB6, then it worked almost like this:

  1. Create directory objects for source and destination
  2. source and Make a file entry object for the destination
  3. Repeat through the source object, and check to see if this destination is
    • if not, then create it
    • If so, see if the source version is new

So far, I have it:

  private bool Sync Files (String Sources, String Destpath) {DirectoryInfo Source = New Directory Info (sourcePath); DirectoryInfo dest = New Directory Info (destPath); If (! Source.Exists) {logline ("source folder not found!"); return false; } If (! Dest.Exists) {logline ("Destination folder not found!"); return false; } FileInfo [] sourceFiles = source.GetFiles (); FileInfo [] destFiles = dest.GetFiles (); Foreach (fileInfo file in sourcefiles) exists on the // // check file if (optRecursive.Checked) {foreach (in source.GetDirectories ()) directory {// create-if-not-exist-destination subdirectory syncFiles (sourcePath + SubDir .name, Destpath + Subdir .name); True} true; }  

I have read examples that look for lawyers to use FileInfo or DirectoryInfo objects to check with "existent" property, but I have an existing archive / list of files , And for every file the file is not checked in the system, because I'm doing this in the network and continuously going back to a multi-thousand-file directory slow slow.

Thanks in Advance.

GetFiles () The method only exists in the file you have existed. It does not make random files that do not exist. So you have to see if it is present in another list or not.

Some rows of this can work:

  var sourceFiles = source.GetFiles (); Var destFiles = dest.GetFiles (); Foreach (var file in sourceFiles) {if! (DestFiles.Any (x = & gt; x.Name == file.Name)) {// whatever}}  

< Strong> Note: You definitely have no guarantee that nothing has changed since you made a call on GetFiles () For example, if you followed it If you try to copy, the file could be deleted or renamed.


Perhaps using the method or something similar can be done in any way. For example, something like this:

  var sourceFiles = source.GetFiles (); Var destFiles = dest.GetFiles (); Var sourceFilesMissingInDestination = sourceFiles.Except (destFiles, new FileNameComparer ()); Foreach (var file in sourceFilesMissingInDestination)  

where the filename compiler is implemented like this:

  public class FileNameComparer : IEqualityComparer & lt; FileInfo & gt; {Public Bowl Eules (FileInfo X, File Info Y) {return equal (x.Name, y.Name); } GetHashCode full full (FileInfo obj) {return obj.Name.GetHashCode (); }}  

Unsterster though: p


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

jquery - SimpleModal Confirm fails to submit form -