Пример #1
0
        private static void FileWatcher(string sourceDirName, string destSourceDirName, string destTempDirName)
        {
            Watcher.SourceDirectoryCopy(sourceDirName, destSourceDirName, true);
            Watcher.TempDirectoryCopy(sourceDirName, destTempDirName, true);

            fileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                       | NotifyFilters.FileName;

            fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
            fileWatcher.Created += new FileSystemEventHandler(OnCreated);
            fileWatcher.Deleted += new FileSystemEventHandler(OnDeleted);
            fileWatcher.Renamed += new RenamedEventHandler(OnRenamed);

            fileWatcher.EnableRaisingEvents   = true;
            fileWatcher.IncludeSubdirectories = true;
            fileWatcher.InternalBufferSize    = 1024 * 1024;
        }
Пример #2
0
        public static void BackupSystem(DateTime dateAndTime, string sourceDirName, string destSourceDirName)
        {
            ClearTrackingFolder(sourceDirName);
            Watcher.SourceDirectoryCopy(destSourceDirName, sourceDirName, true);
            List <StructureEventElement> logItems = new List <StructureEventElement>();

            using (StreamReader logFile = new StreamReader(Constants.LogPath))
            {
                string line;
                while ((line = logFile.ReadLine()) != null)
                {
                    try
                    {
                        var logitem = StructureEventElement.ParseLog(line);

                        if (logitem.TimeOfChanges > dateAndTime)
                        {
                            continue;
                        }
                        else
                        {
                            logItems.Add(logitem);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }

            try
            {
                foreach (var item in logItems)
                {
                    switch (item.TypeOfChanges)
                    {
                    case "Changed":
                    {
                        if (File.Exists(item.PathFile))
                        {
                            File.Delete(item.PathFile);
                        }
                        string temppath = GetTempPath(item);
                        File.Copy(temppath, item.PathFile);
                        break;
                    }

                    case "Created":
                    {
                        if (item.TypeOfObject.Equals("fileWatcher"))
                        {
                            File.Create(item.PathFile).Close();
                            break;
                        }
                        else if (item.TypeOfObject.Equals("dirWatcher"))
                        {
                            Directory.CreateDirectory(item.PathFile);
                            break;
                        }
                        else
                        {
                            throw new ArgumentException();
                        }
                    }

                    case "Deleted":
                    {
                        if (item.TypeOfObject.Equals("fileWatcher"))
                        {
                            File.Delete(item.PathFile);
                            break;
                        }
                        else if (item.TypeOfObject.Equals("dirWatcher"))
                        {
                            Directory.Delete(item.PathFile, true);
                            break;
                        }
                        else
                        {
                            throw new ArgumentException();
                        }
                    }

                    case "Renamed":
                    {
                        if (item.TypeOfObject.Equals("fileWatcher"))
                        {
                            File.Delete(item.OldPathToFile);
                            File.Create(item.PathFile).Close();
                            break;
                        }
                        else if (item.TypeOfObject.Equals("dirWatcher"))
                        {
                            Directory.Delete(item.OldPathToFile, true);
                            Directory.CreateDirectory(item.PathFile);
                            break;
                        }
                        else
                        {
                            throw new ArgumentException();
                        }
                    }

                    default:
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }