Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 487251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:35:09+00:00 2026-05-13T01:35:09+00:00

I am just learning C# (have been fiddling with it for about 2 days

  • 0

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:

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:35:09+00:00Added an answer on May 13, 2026 at 1:35 am

    The GetFiles() method will only get you files that does exist. It doesn’t make up random files that doesn’t exist. So all you have to do is to check if it exists in the other list.

    Something in the lines of this could work:

    var sourceFiles = source.GetFiles();
    var destFiles = dest.GetFiles();
    
    foreach (var file in sourceFiles)
    {
        if(!destFiles.Any(x => x.Name == file.Name))
        {
            // Do whatever
        }
    }
    

    Note: You have of course no guarantee that something hasn’t changed after you have done the calls to GetFiles(). For example, a file could have been deleted or renamed if you try to copy it later.


    Could perhaps be done nicer somehow by using the Except method or something similar. 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)
    {
        // Do whatever
    }
    

    Where the FileNameComparer is implemented like so:

    public class FileNameComparer : IEqualityComparer<FileInfo>
    {
        public bool Equals(FileInfo x, FileInfo y)
        {
            return Equals(x.Name, y.Name);
        }
    
    
        public int GetHashCode(FileInfo obj)
        {
            return obj.Name.GetHashCode();
        }
    }     
    

    Untested though :p

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning about them, and find it discouraging that they have been deprecated.
I'm just learning about python. I'm fairly new. I have the following code that
I'm just beginning to learn Java and I have been very frustrated about learning
I have just been learning about how styles and control templates in WPF can
I have been learning opengl for about 4-5 months now. I am ready to
I have just started learning NHibernate. Over the past few months I have been
I am just learning about app.config in respect of creating custom sections. I have
I am just learning F# and have been converting a library of C# extension
I have been just learning redis and node.js There are two questions I have
I'm 14 and have been learning java for about 4/5 months. I'm coding a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.