The FileSystemWatcher class monitors the change state of the file and backs up the file in real time

First of all, this is what I need to use in a task requirement. The general code is as follows: I encapsulate the method of monitoring files and backup files into a WatcherAndBackup

Class, but I always feel that the encapsulation is not very good. A big bull can point out the correction here. Thank you for your advice! The main class used in the monitoring file is sysytem.IO

Then create an instance of WatcherAndBackup class in a console and run it

 

 1  class WatcherAndBackup
 2     {
 3         string sourcefile = "";//source file
 4         string targetfile = "";//Target file
 5         string targetPath = "";//Target path
 6         public WatcherAndBackup(string Sourcefile,string Targetfile,string TargetPath)
 7         {
 8             sourcefile = Sourcefile;targetfile = Targetfile;targetPath = TargetPath;
 9         }
10        
11         public void backup(string sourcefile,string targetfile,string targetPath)
12         {
13             try
14             {
15                 if (!Directory.Exists(targetPath))
16                 {
17                     Directory.CreateDirectory(targetPath);
18                     
19                 }
20                 File.Copy(sourcefile, targetfile, true);
21 
22             }
23             catch { }
24         }
25         #region Monitor file changes and back up files in real time
26         public void watcherfile(string path,string file)
27         {
28             FileSystemWatcher fsw = new FileSystemWatcher(path, file);
29             fsw.Changed += new FileSystemEventHandler(change_watcher);
30             fsw.Created += new FileSystemEventHandler(change_watcher);
31             fsw.Deleted += new FileSystemEventHandler(change_watcher);
32             fsw.Renamed += new RenamedEventHandler(rename_watcher);
33             fsw.EnableRaisingEvents = true;
34             Console.WriteLine("Start monitoring....");
35             fsw.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName
36                 | NotifyFilters.LastAccess | NotifyFilters.Security | NotifyFilters.Size | NotifyFilters.LastWrite;
37             fsw.IncludeSubdirectories = true;
38         }
39         public void change_watcher(object sender,FileSystemEventArgs e)
40         {
41            
42             var wacher = sender as FileSystemWatcher;
43             wacher.EnableRaisingEvents = false;
44             
45             if (e.ChangeType==WatcherChangeTypes.Changed)
46             {
47                 Console.WriteLine("Backing up...");
48                     backup(sourcefile,targetfile,targetPath);
49                     Console.WriteLine("Backup successful...");
50                                
51             }
52             else if (e.ChangeType==WatcherChangeTypes.Created)
53             {
54                 Console.WriteLine("{0},{1},{2}", e.ChangeType, e.FullPath, e.Name);
55                 return;
56             }
57             else if (e.ChangeType==WatcherChangeTypes.Deleted)
58             {
59                 Console.WriteLine("{0},{1},{2}", e.ChangeType, e.FullPath, e.Name);
60                 return;
61             }
62             wacher.EnableRaisingEvents = true;
63         }
64         public void rename_watcher(object sender,RenamedEventArgs e)
65         {
66             Console.WriteLine("{0},{1},{2}", e.ChangeType, e.FullPath, e.Name);
67         }
68         #endregion
69 
70     }
 static void Main(string[] args)
        {
            WatcherAndBackup bk = new WatcherAndBackup(@"D:\gg\config.xml", @"D:\gg\backup\config.xml", @"D:\gg\backup");
            bk.watcherfile(@"D:\gg", "config.xml");//The monitored file is D:\gg\config.xml
            Console.Read();         
        }

Here's an explanation: when the instance class WatcherAndBackup, write down three parameters of the backup method: sourcefile, targefile, and targePath, that is, the directory of the source file, target file, and target file of the backup method, and then why there are these codes in the change'watcher method:

var wacher=sender as FileSystemWatcher;

wacher.EnableRaisingEvents=false;

Then after the method:

wacher.EnableRaisingEvents=true;

In fact, if you don't add these lines of code, it will appear that the changed method will be triggered twice when file modification is monitored. This modification method is a modification method I found on the Internet

Well, I'm almost finished... If there is anything wrong, please correct it. I wrote it with the attitude of study.. hey!!



Tags: C# xml

Posted on Sun, 03 May 2020 23:30:24 -0700 by Richard Bowser