浏览代码

修改文件监测变动的工具

yuzhengyang 8 年之前
父节点
当前提交
fda73320c4

+ 0 - 26
Fork.Net/Oreo.Plugins/Oreo.FileMan/Partials/FileBackupPartial.Designer.cs

@@ -38,9 +38,7 @@
             this.DgvFileSize = new System.Windows.Forms.DataGridViewTextBoxColumn();
             this.DgvFileVersionHistory = new System.Windows.Forms.DataGridViewTextBoxColumn();
             this.DgvFileLastBackupTime = new System.Windows.Forms.DataGridViewTextBoxColumn();
-            this.BtStart = new System.Windows.Forms.Button();
             this.BtDelPath = new System.Windows.Forms.Button();
-            this.BtStop = new System.Windows.Forms.Button();
             ((System.ComponentModel.ISupportInitialize)(this.DgvPath)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.DgvFile)).BeginInit();
             this.SuspendLayout();
@@ -142,16 +140,6 @@
             this.DgvFileLastBackupTime.ReadOnly = true;
             this.DgvFileLastBackupTime.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
             // 
-            // BtStart
-            // 
-            this.BtStart.Location = new System.Drawing.Point(481, 6);
-            this.BtStart.Name = "BtStart";
-            this.BtStart.Size = new System.Drawing.Size(75, 23);
-            this.BtStart.TabIndex = 3;
-            this.BtStart.Text = "启动";
-            this.BtStart.UseVisualStyleBackColor = true;
-            this.BtStart.Click += new System.EventHandler(this.BtStart_Click);
-            // 
             // BtDelPath
             // 
             this.BtDelPath.Location = new System.Drawing.Point(99, 6);
@@ -162,23 +150,11 @@
             this.BtDelPath.UseVisualStyleBackColor = true;
             this.BtDelPath.Click += new System.EventHandler(this.BtDelPath_Click);
             // 
-            // BtStop
-            // 
-            this.BtStop.Location = new System.Drawing.Point(562, 6);
-            this.BtStop.Name = "BtStop";
-            this.BtStop.Size = new System.Drawing.Size(75, 23);
-            this.BtStop.TabIndex = 7;
-            this.BtStop.Text = "停止";
-            this.BtStop.UseVisualStyleBackColor = true;
-            this.BtStop.Click += new System.EventHandler(this.BtStop_Click);
-            // 
             // FileBackupPartial
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.Controls.Add(this.BtStop);
             this.Controls.Add(this.BtDelPath);
-            this.Controls.Add(this.BtStart);
             this.Controls.Add(this.DgvFile);
             this.Controls.Add(this.BtAddPath);
             this.Controls.Add(this.DgvPath);
@@ -196,7 +172,6 @@
         private System.Windows.Forms.DataGridView DgvPath;
         private System.Windows.Forms.Button BtAddPath;
         private System.Windows.Forms.DataGridView DgvFile;
-        private System.Windows.Forms.Button BtStart;
         private System.Windows.Forms.Button BtDelPath;
         private System.Windows.Forms.DataGridViewTextBoxColumn DgvPathName;
         private System.Windows.Forms.DataGridViewTextBoxColumn DgvPathSize;
@@ -205,6 +180,5 @@
         private System.Windows.Forms.DataGridViewTextBoxColumn DgvFileSize;
         private System.Windows.Forms.DataGridViewTextBoxColumn DgvFileVersionHistory;
         private System.Windows.Forms.DataGridViewTextBoxColumn DgvFileLastBackupTime;
-        private System.Windows.Forms.Button BtStop;
     }
 }

+ 5 - 13
Fork.Net/Oreo.Plugins/Oreo.FileMan/Partials/FileBackupPartial.cs

@@ -24,7 +24,7 @@ namespace Oreo.FileMan.Partials
     public partial class FileBackupPartial : UserControl
     {
         FileWatcher Watcher = new FileWatcher();
-        string FileManBackup = @"F:\FileManBackup\";
+        string FileManBackup = @"G:\FileManBackup\";
         List<BackupPaths> Paths = new List<BackupPaths>();
         List<string> BackupFiles = new List<string>();
         DispatcherTimer Timer = new DispatcherTimer();
@@ -37,7 +37,6 @@ namespace Oreo.FileMan.Partials
         private void FileBackupPartial_Load(object sender, EventArgs e)
         {
             Watcher.eventHandler += WatcherChangedEvent;
-            Watcher.Start();
             BackupFileTask();
 
             //读取要备份的文件路径列表
@@ -48,9 +47,10 @@ namespace Oreo.FileMan.Partials
                     Paths = db.GetAll<BackupPaths>(null, false).ToList();
                     if (ListTool.HasElements(Paths))
                     {
-                        foreach (var b in Paths)
+                        foreach (var p in Paths)
                         {
-                            UIDgvPathAdd(DirTool.GetPathName(b.Path));
+                            Watcher.Add(p.Path, true);
+                            UIDgvPathAdd(DirTool.GetPathName(p.Path));
                         }
                     }
                 }
@@ -89,6 +89,7 @@ namespace Oreo.FileMan.Partials
                                 if (db.Add(bp) > 0)
                                 {
                                     Paths.Add(bp);//添加到列表
+                                    Watcher.Add(bp.Path, true);//添加到监听
                                     UIDgvPathAdd(name);//添加到列表UI
 
                                     long size = 0;//目录下的文件大小
@@ -148,15 +149,6 @@ namespace Oreo.FileMan.Partials
                 });
             }
         }
-
-        private void BtStart_Click(object sender, EventArgs e)
-        {
-            Watcher.Start();
-        }
-        private void BtStop_Click(object sender, EventArgs e)
-        {
-            Watcher.Stop();
-        }
         private void WatcherChangedEvent(object sender, FileWatcherEventArgs e)
         {
             if (Paths.Any(x => e.FullPath.Contains(x.Path)))

+ 0 - 21
Fork.Net/Oreo.Plugins/Oreo.FileMan/Partials/FileBackupPartial.resx

@@ -123,27 +123,6 @@
   <metadata name="DgvPathSize.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
     <value>True</value>
   </metadata>
-  <metadata name="DgvPathName.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
-    <value>True</value>
-  </metadata>
-  <metadata name="DgvPathSize.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
-    <value>True</value>
-  </metadata>
-  <metadata name="DgvFileName.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
-    <value>True</value>
-  </metadata>
-  <metadata name="DgvFilePath.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
-    <value>True</value>
-  </metadata>
-  <metadata name="DgvFileSize.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
-    <value>True</value>
-  </metadata>
-  <metadata name="DgvFileVersionHistory.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
-    <value>True</value>
-  </metadata>
-  <metadata name="DgvFileLastBackupTime.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
-    <value>True</value>
-  </metadata>
   <metadata name="DgvFileName.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
     <value>True</value>
   </metadata>

+ 52 - 16
Fork.Net/Y.Utils/IOUtils/FileManUtils/FileWatcher.cs

@@ -38,29 +38,65 @@ namespace Y.Utils.IOUtils.FileManUtils
         /// 当前运行状态
         /// </summary>
         public bool IsStart { get { return _IsStart; } }
-        /// <summary>
-        /// 初始化文件监测
-        /// </summary>
+
+
         public FileWatcher()
         {
-            DriveInfo[] drives = DriveInfo.GetDrives().Where(x => x.IsReady && (x.DriveType == DriveType.Fixed || x.DriveType == DriveType.Removable)).ToArray();
-            if (ListTool.HasElements(drives))
+            //DriveInfo[] drives = DriveInfo.GetDrives().Where(x => x.IsReady && (x.DriveType == DriveType.Fixed || x.DriveType == DriveType.Removable)).ToArray();
+            //if (ListTool.HasElements(drives))
+            //{
+            //    foreach (var d in drives)
+            //    {
+            //        //if (d.Name.Contains("C")) continue;
+            //        FileSystemWatcher fsw = new FileSystemWatcher(d.Name);
+            //        fsw.Created += CreatedEvent;//创建文件或目录
+            //        fsw.Changed += ChangedEvent;//更改文件或目录
+            //        fsw.Deleted += DeletedEvent;//删除文件或目录
+            //        fsw.Renamed += RenamedEvent;//重命名文件或目录
+            //        fsw.Error += ErrorEvent;
+            //        fsw.IncludeSubdirectories = true;
+            //        fsw.NotifyFilter = (NotifyFilters)383;
+            //        Watchers.Add(fsw);
+            //    }
+            //}
+        }
+        public FileWatcher(string[] paths)
+        {
+            if (ListTool.HasElements(paths))
+            {
+                foreach (var p in paths)
+                {
+                    Add(p);
+                }
+            }
+        }
+
+        public void Add(string path, bool start = false)
+        {
+            FileSystemWatcher fsw = new FileSystemWatcher(path);
+            fsw.Created += CreatedEvent;//创建文件或目录
+            fsw.Changed += ChangedEvent;//更改文件或目录
+            fsw.Deleted += DeletedEvent;//删除文件或目录
+            fsw.Renamed += RenamedEvent;//重命名文件或目录
+            fsw.Error += ErrorEvent;
+            fsw.IncludeSubdirectories = true;
+            fsw.NotifyFilter = (NotifyFilters)383;
+            if (start) fsw.EnableRaisingEvents = start;
+            Watchers.Add(fsw);
+        }
+        public void Remove(string path)
+        {
+            for (int i = Watchers.Count - 1; i >= 0; i--)
             {
-                foreach (var d in drives)
+                if (Watchers[i].Path == path)
                 {
-                    //if (d.Name.Contains("C")) continue;
-                    FileSystemWatcher fsw = new FileSystemWatcher(d.Name);
-                    fsw.Created += CreatedEvent;//创建文件或目录
-                    fsw.Changed += ChangedEvent;//更改文件或目录
-                    fsw.Deleted += DeletedEvent;//删除文件或目录
-                    fsw.Renamed += RenamedEvent;//重命名文件或目录
-                    fsw.Error += ErrorEvent;
-                    fsw.IncludeSubdirectories = true;
-                    fsw.NotifyFilter = (NotifyFilters)383;
-                    Watchers.Add(fsw);
+                    Watchers[i].EnableRaisingEvents = false;
+                    Watchers[i].Dispose();
+                    Watchers.RemoveAt(i);
                 }
             }
         }
+
         /// <summary>
         /// 启动文件监测
         /// </summary>