ソースを参照

完善文件备份中的文件变更监测

yuzhengyang 8 年 前
コミット
d3bcc556aa

+ 19 - 11
Fork.Net/Oreo.Plugins/Oreo.FileMan/Partials/FileBackupPartial.cs

@@ -14,6 +14,7 @@ using Y.Utils.DataUtils.UnitConvertUtils;
 using Y.Utils.IOUtils.PathUtils;
 using Oreo.FileMan.Models;
 using Oreo.FileMan.DatabaseEngine;
+using Y.Utils.IOUtils.FileManUtils;
 
 namespace Oreo.FileMan.Partials
 {
@@ -29,7 +30,7 @@ namespace Oreo.FileMan.Partials
         }
         private void FileBackupPartial_Load(object sender, EventArgs e)
         {
-            Watcher. += WatcherChangedEvent;
+            Watcher.eventHandler += WatcherChangedEvent;
             //读取要备份的文件路径列表
             Task.Factory.StartNew(() =>
             {
@@ -53,36 +54,39 @@ namespace Oreo.FileMan.Partials
             dialog.Description = "请选择要备份的文件夹";
             if (dialog.ShowDialog() == DialogResult.OK)
             {
-                string selPath = dialog.SelectedPath;//格式化选中的目录
-                List<BackupPaths> clashPath = Paths.Where(x => x.Path.Contains(selPath + "\\") || (selPath + "\\").Contains(x.Path)).ToList();//查询冲突项
+                string selPath = dialog.SelectedPath;//获取选中的目录 
+                string path = DirTool.Combine(selPath, "\\");//格式化选中的目录
+                string name = Path.GetFileName(selPath);//获取目录名称
+
+                List<BackupPaths> clashPath = Paths.Where(x => x.Path.Contains(path) || path.Contains(x.Path)).ToList();//查询冲突项
                 if (ListTool.HasElements(clashPath))
                 {
                     string cp = "";
                     clashPath.ForEach(x => cp += (x.Path + ";"));
                     //存在重合目录
-                    MessageBox.Show(string.Format("您当前选择路径:{0},与之前选择的目录:{1},存在嵌套包含关系,请先从备份目录中移除,然后重新添加。", selPath, cp));
+                    MessageBox.Show(string.Format("您当前选择路径:{0},与之前选择的目录:{1},存在嵌套包含关系,请先从备份目录中移除,然后重新添加。", path, cp));
                 }
                 else
                 {
                     long size = 0;//目录下的文件大小
                     int row = DgvPath.Rows.Count;//当前目录列表总数
-                    BackupPaths bp = new BackupPaths() { Name = Path.GetFileName(selPath), Path = selPath + "\\", };
+                    BackupPaths bp = new BackupPaths() { Name = name, Path = path, };
                     Paths.Add(bp);//添加到列表
-                    UIDgvPathAdd(Path.GetFileName(selPath));//添加到列表UI
+                    UIDgvPathAdd(name);//添加到列表UI
 
                     UIEnableButton(false);
                     Task.Factory.StartNew(() =>
                     {
                         using (var db = new Muse())
                         {
-                            if (!db.Do<BackupPaths>().Any(x => x.Path == (selPath + "\\"))) db.Add(bp);//添加到数据库
-                            List<string> files = FileTool.GetAllFile(selPath);
+                            if (!db.Do<BackupPaths>().Any(x => x.Path == path)) db.Add(bp);//添加到数据库
+                            List<string> files = FileTool.GetAllFile(path);
                             if (ListTool.HasElements(files))
                             {
                                 foreach (var f in files)
                                 {
                                     size += FileTool.Size(f);
-                                    UIDgvPathUpdate(row, Path.GetFileName(selPath), ByteConvertTool.Fmt(size));//更新目录文件大小
+                                    UIDgvPathUpdate(row, name, ByteConvertTool.Fmt(size));//更新目录文件大小
                                 }
                             }
                         }
@@ -139,9 +143,13 @@ namespace Oreo.FileMan.Partials
         {
             Watcher.Stop();
         }
-        private void WatcherChangedEvent(object sender, FileSystemEventArgs e)
+        private void WatcherChangedEvent(object sender, FileWatcherEventArgs e)
         {
-            UIDgvFileAdd(e.Name, e.FullPath, "Changed");
+            if (Paths.Any(x => e.FullPath.Contains(x.Path)))
+            {
+                //FileTool.IsFile(e.FullPath)
+                UIDgvFileAdd(e.Name, e.FullPath, e.ChangeType.ToString());
+            }
         }
 
         /// <summary>

+ 43 - 7
Fork.Net/Y.Utils/IOUtils/FileManUtils/FileWatcher.cs

@@ -1,4 +1,12 @@
-using System;
+//************************************************************************
+//      https://github.com/yuzhengyang
+//      author:     yuzhengyang
+//      date:       2017.6.28 - 2017.6.29
+//      desc:       文件变更监测
+//      Copyright (c) yuzhengyang. All rights reserved.
+//************************************************************************
+
+using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
@@ -12,6 +20,17 @@ namespace Y.Utils.IOUtils.FileManUtils
     /// </summary>
     public class FileWatcher
     {
+        /// <summary>
+        /// 接受文件监控信息的事件委托
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="args"></param>
+        public delegate void FileWatcherEventHandler(object sender, FileWatcherEventArgs args);
+        /// <summary>
+        /// 获取文件监控信息
+        /// </summary>
+        public FileWatcherEventHandler eventHandler;
+
         private bool _IsStart = false;
         private List<FileSystemWatcher> Watchers = new List<FileSystemWatcher>();
 
@@ -19,7 +38,9 @@ namespace Y.Utils.IOUtils.FileManUtils
         /// 当前运行状态
         /// </summary>
         public bool IsStart { get { return _IsStart; } }
-        public delegate void FileWatcherEventHandler(object sender, FileWatcherEventArgs args);
+        /// <summary>
+        /// 初始化文件监测
+        /// </summary>
         public FileWatcher()
         {
             DriveInfo[] drives = DriveInfo.GetDrives().Where(x => x.IsReady && (x.DriveType == DriveType.Fixed || x.DriveType == DriveType.Removable)).ToArray();
@@ -27,6 +48,7 @@ namespace Y.Utils.IOUtils.FileManUtils
             {
                 foreach (var d in drives)
                 {
+                    //if (d.Name.Contains("C")) continue;
                     FileSystemWatcher fsw = new FileSystemWatcher(d.Name);
                     fsw.Created += CreatedEvent;//创建文件或目录
                     fsw.Changed += ChangedEvent;//更改文件或目录
@@ -38,6 +60,9 @@ namespace Y.Utils.IOUtils.FileManUtils
                 }
             }
         }
+        /// <summary>
+        /// 启动文件监测
+        /// </summary>
         public void Start()
         {
             _IsStart = true;
@@ -49,6 +74,9 @@ namespace Y.Utils.IOUtils.FileManUtils
                 }
             }
         }
+        /// <summary>
+        /// 停止文件监测
+        /// </summary>
         public void Stop()
         {
             _IsStart = false;
@@ -62,21 +90,29 @@ namespace Y.Utils.IOUtils.FileManUtils
         }
 
 
-        private void CreatedEvent(object sender, FileSystemEventArgs e)
+
+        private void DriveMonitor()
         {
+            //监测磁盘的插入拔出
+
+        }
 
+
+        private void CreatedEvent(object sender, FileSystemEventArgs e)
+        {
+            eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
         }
         private void ChangedEvent(object sender, FileSystemEventArgs e)
         {
-
+            eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
         }
         private void DeletedEvent(object sender, FileSystemEventArgs e)
         {
-
+            eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
         }
         private void RenamedEvent(object sender, RenamedEventArgs e)
         {
-
+            eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), e.OldFullPath, e.OldName));
         }
-    } 
+    }
 }

+ 23 - 5
Fork.Net/Y.Utils/IOUtils/FileManUtils/FileWatcherEventArgs.cs

@@ -8,10 +8,28 @@ namespace Y.Utils.IOUtils.FileManUtils
 {
     public class FileWatcherEventArgs
     {
-        public WatcherChangeTypes ChangeType { get; }
-        public string FullPath { get; }
-        public string Name { get; }
-        public string OldFullPath { get; }
-        public string OldName { get; }
+        public FileWatcherEventArgs(WatcherChangeTypes type, string fullpath, string name, string oldfullpath, string oldname)
+        {
+            _ChangeType = type;
+            _FullPath = fullpath;
+            _Name = name;
+            _OldFullPath = oldfullpath;
+            _OldName = oldname;
+        }
+
+        private WatcherChangeTypes _ChangeType;
+        public WatcherChangeTypes ChangeType { get { return _ChangeType; } }
+
+        private string _FullPath;
+        public string FullPath { get { return _FullPath; } }
+
+        private string _Name;
+        public string Name { get { return _Name; } }
+
+        private string _OldFullPath;
+        public string OldFullPath { get { return _OldFullPath; } }
+
+        private string _OldName;
+        public string OldName { get { return _OldName; } }
     }
 }

+ 11 - 1
Fork.Net/Y.Utils/IOUtils/FileUtils/FileTool.cs

@@ -1,7 +1,7 @@
 //************************************************************************
 //      https://github.com/yuzhengyang
 //      author:     yuzhengyang
-//      date:       2017.3.29 - 2017.6.20
+//      date:       2017.3.29 - 2017.6.29
 //      desc:       文件操作工具
 //      Copyright (c) yuzhengyang. All rights reserved.
 //************************************************************************
@@ -22,6 +22,16 @@ namespace Y.Utils.IOUtils.FileUtils
     public class FileTool
     {
         /// <summary>
+        /// 判断字符串是文件路径
+        /// </summary>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public static bool IsFile(string s)
+        {
+            if (File.Exists(s)) return true;
+            return false;
+        }
+        /// <summary>
         /// 获取文件(单层目录)
         /// </summary>
         /// <param name="path">路径</param>