The c synchronization two subdirectory file example shares two folder synchronization

  • 2020-06-03 08:07:44
  • OfStack


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace AutoSync
{
    public class NewDirectory
    {
        public static Dictionary<string,string> GetDirectories(string dirname)
        {
            Dictionary<string, string> dirs = new Dictionary<string, string>();
            string[] strDirs = Directory.GetDirectories(dirname);
            foreach (string str in strDirs)
            {
                string[] result = str.Split('\\');
                dirs.Add(result[result.Length-1], result[result.Length-1]);
            }
            return dirs;
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace AutoSync
{
    enum SyncResult
    {
        Success,SourceDirNotExists,DestDirNotExists
    }
    class FloderSync
    {
        public int CreateDirCount = 0;
        public int CopyFileCount = 0;
        public List<string> Log = new List<string>();
        private void AddLog(string logtext)
        {
            if (Log.Count < 1000)
                Log.Add(System.DateTime.Now.ToString() + logtext);
            else if (Log.Count == 1000)
                Log.Add(System.DateTime.Now.ToString() + "   Reach log limit , No additional ");
        }
        public SyncResult StartSync(string sourcedir, string destdir)
        {
            // Pass in directory name protection 
            sourcedir = sourcedir.Trim();
            destdir = destdir.Trim();
            // At the end of the guarantee list 1 Characters are not slashes 
            if (sourcedir[sourcedir.Length - 1] == '\\')
                sourcedir = sourcedir.Remove(sourcedir.Length - 1);
            if (destdir[destdir.Length - 1] == '\\')
                destdir = destdir.Remove(destdir.Length - 1);
            // Determine if the directory exists 
            if (!Directory.Exists(sourcedir)) return SyncResult.SourceDirNotExists;
            if (!Directory.Exists(destdir)) return SyncResult.DestDirNotExists;
            // Gets directory information in the source and destination directories 
            Dictionary<string, string> SDirInfo = new Dictionary<string, string>();
            Dictionary<string, string> DDirInfo = new Dictionary<string, string>();
            Dictionary<string, string> aa = new Dictionary<string, string>();
            SDirInfo = NewDirectory.GetDirectories(sourcedir);// Gets directory information for the source directory 
            DDirInfo = NewDirectory.GetDirectories(destdir);// Gets directory information for the target directory 
            //
            //       Start synchronizing the two directories, but only synchronize the source directory information first 
            //------ Compare the subdirectory information in the two directories ---------------------
            foreach (KeyValuePair<string, string> kvp in SDirInfo) // Searches for directories that do not exist in the target directory but do not exist in the source directory 
            {
                if(!DDirInfo.ContainsKey(kvp.Key)) // If no directory exists in the target directory, it is created immediately 
                {
                    string dirname=destdir + "\\" + kvp.Key;
                    Directory.CreateDirectory(dirname);
                    AddLog("   Create directory: " + dirname);

                    CreateDirCount++;
                }
                // The directory synchronization function is recursively called to implement nested directories 1 Subsexual full synchronization 
                StartSync(sourcedir + "\\" + kvp.Key, destdir + "\\" + kvp.Key);
            }
            // Gets a list of all files in the source directory 
            string[] SFiles = Directory.GetFiles(sourcedir);
            //string[] DFiles = Directory.GetFiles(destdir);
            //------ Compare the file information in the two directories (this layer directory) --------------
            foreach (string sfilename in SFiles)
            {
                string dfilename = destdir+"\\"+Path.GetFileName(sfilename);
                if (File.Exists(dfilename)) // If a file with the same name already exists in the destination directory, compare its last modification time and take the latest 
                {
                    // Gets file information for a file of the same name in the source and destination directories 
                    FileInfo sfi = new FileInfo(sfilename);
                    FileInfo dfi = new FileInfo(dfilename);
                    // If the source file is longer than the destination file modification time 5 Seconds or more to copy overwrite the past, mainly for the operating system 1 Some differences, for the same modification time but different file size files are not processed 
                    if (sfi.LastWriteTime > dfi.LastWriteTime.AddSeconds(5))
                    {
                        // Copy the same name file from the source directory to the destination directory 
                        File.Copy(sfilename, dfilename, true);
                        AddLog("   Overwritten file: " + dfilename);
                        CopyFileCount++;
                    }
                }
                else // If a file with the same name does not exist in the destination directory, copy it 
                {
                    // Copy the same name file from the source directory to the destination directory 
                    File.Copy(sfilename, dfilename, true);
                    AddLog("   Copy file: " + dfilename);
                    CopyFileCount++;
                }
            }
            return SyncResult.Success;
        }
    }
}


Related articles: