views:

175

answers:

1

I am just learning C# (have been fiddling with it for about 2 days now) and I've decided that, for leaning purposes, I will rebuild an old app I made in VB6 for syncing files (generally across a network).

When I wrote the code in VB 6, it worked approximately like this:

  1. Create a Scripting.FileSystemObject
  2. Create directory objects for the source and destination
  3. Create file listing objects for the source and destination
  4. Iterate through the source object, and check to see if it exists in the destination
    • if not, create it
    • if so, check to see if the source version is newer/larger, and if so, overwrite the other

So far, this is what I have. Also, if I'm making any horribly retarded mistakes, please let me know, as I am a total scrub at this point.

private bool syncFiles(string sourcePath, string destPath) {
    DirectoryInfo source = new DirectoryInfo(sourcePath);
    DirectoryInfo dest = new DirectoryInfo(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) {
        // check exists on file
    }

    if (optRecursive.Checked) {
        foreach (DirectoryInfo subDir in source.GetDirectories()) {
            // create-if-not-exists destination subdirectory
            syncFiles(sourcePath + subDir.Name, destPath + subDir.Name);
        }
    }
    return true;
}

I have read examples that seem to advocate using the FileInfo or DirectoryInfo objects to do checks with the "Exists" property, but I am specifically looking for a way to search an existing collection/list of files, and not live checks to the file system for each file, since I will be doing so across the network and constantly going back to a multi-thousand-file directory is slow slow slow.

Thanks in Advance.

+3  A: 
Svish
Also for the sub directories use Path.Combine(sourcePath,subDir.Name) instead of sourcePath + subDir.Name
astander
Also have a look at the GetFileSystemInfos method: http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.getfilesysteminfos.aspx
Svish
As to your note: I understand, but it's a risk I'm taking, frontloading the operation of getting the file list so I don't have to do individual exists checks on umpteen-thousand files. Thanks very much for your answer, I'm off to check out the syntax of "Any"!
Dereleased
It's faster to get all destination files from a DirectoryInfo. Don't call Exists on all your destination files, but do check if file open calls succeed because files can be deleted or renamed, just like Svish explained.
Sebastiaan Megens
@Martinho: That is a great idea. Was trying to come up with something clever like that, but my brain just failed me :p
Svish
@Martinho: Although you might need to implement your own equality comparer or something, since `Except` will compare the `FileInfo`s directly. And they probably won't be equal since they are in different directories ;-)
Svish