Ejemplo n.º 1
0
 /// <summary>
 /// Stop the file synchronization
 /// </summary>
 public void Stop()
 {
     if (!Running)
     {
         var err = new FileSyncException("Filesync is not running.");
         if (ErrorOccured != null)
         {
             ErrorOccured(this, err);
         }
         return;
     }
     if (fromWatcher != null)
     {
         fromWatcher.EnableRaisingEvents = false;
         fromWatcher = null;
     }
     if (toWatcher != null)
     {
         toWatcher.EnableRaisingEvents = false;
         toWatcher = null;
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Start the file synchronization
        /// </summary>
        /// <param name="twoWaySync">when <c>true</c>, apply changes in the destination path back to the source path. when <c>false</c>, just apply any source changes to the destination</param>
        public void Start(bool twoWaySync = false)
        {
            if (Running)
            {
                var err = new FileSyncException("Filesync is already running.");
                if (ErrorOccured != null)
                {
                    ErrorOccured(this, err);
                }
                return;
            }
            UNCAccessWithCredentials unc = null;

            if (toPath.IsUNC)
            {
                unc = new UNCAccessWithCredentials()
                {
                    AutoDispose = false
                };
                if (toPath.IsPathHasUserName && (!unc.NetUseWithCredentials(toPath.Path.Trim().TrimEnd('\\'), toPath.UserName, toPath.Domain, toPath.Password) && unc.LastError != 1219))
                {
                    if (!toPath.Path.StartsWith(@"\\10.10."))
                    {
                        if (ErrorOccured != null)
                        {
                            ErrorOccured(this, new Exception("Failed to connect to " + toPath + "\r\nYou have to point to remote share folder using IP address instead of DNS name, So Remote Share folder should look like:\\\\10.10..\\SharefolderName."));
                        }
                        else
                        if (ErrorOccured != null)
                        {
                            ErrorOccured(this, new Exception("Failed to connect to " + toPath + "\r\nLastError = " + unc.LastError.ToString()));
                        }
                    }
                    // return;
                }
            }

            UNCAccessWithCredentials unc_from = null;

            if (fromPath.IsUNC)
            {
                unc_from = new UNCAccessWithCredentials()
                {
                    AutoDispose = false
                };
                if (fromPath.IsPathHasUserName && (!unc_from.NetUseWithCredentials(fromPath.Path.Trim().TrimEnd('\\'), fromPath.UserName, fromPath.Domain, fromPath.Password) && unc.LastError != 1219))
                {
                    if (!fromPath.Path.StartsWith(@"\\10.10."))
                    {
                        if (ErrorOccured != null)
                        {
                            ErrorOccured(this, new Exception("Failed to connect to " + fromPath + "\r\nYou have to point to remote share folder using IP address instead of DNS name, So Remote Share folder should look like:\\\\10.10..\\SharefolderName."));
                        }
                        else
                        if (ErrorOccured != null)
                        {
                            ErrorOccured(this, new Exception("Failed to connect to " + fromPath + "\r\nLastError = " + unc_from.LastError.ToString()));
                        }
                    }
                    // return ;
                }
            }
            fromWatcher = new FileSystemWatcher(fromPath.Path);
            fromWatcher.InternalBufferSize    = 65536;
            fromWatcher.IncludeSubdirectories = true;
            fromWatcher.NotifyFilter          = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite | NotifyFilters.CreationTime;
            // Add event handlers.
            fromWatcher.Changed += new FileSystemEventHandler(OnChanged);
            fromWatcher.Created += new FileSystemEventHandler(OnChanged);
            fromWatcher.Deleted += new FileSystemEventHandler(OnChanged);
            fromWatcher.Renamed += new RenamedEventHandler(OnRenamed);
            fromWatcher.Error   += fromWatcher_Error;
            // start watching
            fromWatcher.EnableRaisingEvents = true;

            if (twoWaySync)
            {
                toWatcher = new FileSystemWatcher(toPath.Path);
                toWatcher.InternalBufferSize    = 65536;
                toWatcher.IncludeSubdirectories = true;
                toWatcher.NotifyFilter          = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite | NotifyFilters.CreationTime;
                // Add event handlers.
                toWatcher.Changed += new FileSystemEventHandler(OnChanged);
                toWatcher.Created += new FileSystemEventHandler(OnChanged);
                toWatcher.Deleted += new FileSystemEventHandler(OnChanged);
                toWatcher.Renamed += new RenamedEventHandler(OnRenamed);
                toWatcher.Error   += toWatcher_Error;

                // start watching
                toWatcher.EnableRaisingEvents = true;
            }
        }