ソースを参照

进度巨慢的文件备份

yuzhengyang 8 年 前
コミット
ded470bb98

+ 2 - 6
Fork.Net/Oreo.Plugins/Oreo.FaultLog/Partials/FaultLogInputPartial.cs

@@ -98,6 +98,8 @@ namespace Oreo.FaultLog.Partials
                 Postscript = TbPostscript.Text,
                 System = CbSystem.Text,
                 CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
+                IsFinish = CbIsFinish.Checked,
+                FinishTime = CbIsFinish.Checked ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : "",
             };
 
             UICleanInput();
@@ -107,12 +109,6 @@ namespace Oreo.FaultLog.Partials
                 UIAddButton(false);
                 using (var db = new Muse())
                 {
-                    if (CbIsFinish.Checked)
-                    {
-                        fl.IsFinish = true;
-                        fl.FinishTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
-                    }
-
                     db.Add(fl);
 
                     var fls = db.Do<FaultLogs>().SqlQuery("SELECT * FROM faultlogs WHERE createtime LIKE @p0", DateTime.Now.ToString("yyyy-MM-dd") + "%");

+ 2 - 1
Fork.Net/Oreo.Plugins/Oreo.FaultLog/Partials/FaultLogModifyPartial.cs

@@ -55,12 +55,13 @@ namespace Oreo.FaultLog.Partials
                     fl.Problem = TbProblem.Text;
                     fl.Solution = TbSolution.Text;
                     fl.System = CbSystem.Text;
+                  
                     if (CbIsFinish.Checked && !fl.IsFinish)
                     {
                         fl.IsFinish = true;
                         fl.FinishTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                     }
-                    else
+                    if (!CbIsFinish.Checked && fl.IsFinish)
                     {
                         fl.IsFinish = false;
                         fl.FinishTime = "";

+ 5 - 2
Fork.Net/Oreo.Plugins/Oreo.FileMan/Models/BackupFiles.cs

@@ -8,9 +8,12 @@ namespace Oreo.FileMan.Models
     public class BackupFiles
     {
         public int Id { get; set; }
-        public string FilePath { get; set; }
+        public string Name { get; set; }
+        public string Path { get; set; }
+        public string FullPath { get; set; }
+        public string  BackupName { get; set; }
+        public string BackupFullPath { get; set; }
         public long Size { get; set; }
         public string UpdateTime { get; set; }
-        public string BackupFileName { get; set; }
     }
 }

+ 1 - 0
Fork.Net/Oreo.Plugins/Oreo.FileMan/Oreo.FileMan.csproj

@@ -85,6 +85,7 @@
     <Reference Include="System.Drawing" />
     <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml" />
+    <Reference Include="WindowsBase" />
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Commons\R.cs" />

+ 66 - 3
Fork.Net/Oreo.Plugins/Oreo.FileMan/Partials/FileBackupPartial.cs

@@ -15,14 +15,20 @@ using Y.Utils.IOUtils.PathUtils;
 using Oreo.FileMan.Models;
 using Oreo.FileMan.DatabaseEngine;
 using Y.Utils.IOUtils.FileManUtils;
+using System.Windows.Threading;
+using System.Threading;
+using Y.Utils.DataUtils.DateTimeUtils;
 
 namespace Oreo.FileMan.Partials
 {
     public partial class FileBackupPartial : UserControl
     {
         FileWatcher Watcher = new FileWatcher();
-        string FileManBackup = @"D:\FileManBackup\";
+        string FileManBackup = @"G:\FileManBackup\";
         List<BackupPaths> Paths = new List<BackupPaths>();
+        List<string> BackupFiles = new List<string>();
+        DispatcherTimer Timer = new DispatcherTimer();
+        int BACK_UP_INTERVAL = 5 * 1000;
 
         public FileBackupPartial()
         {
@@ -31,6 +37,9 @@ namespace Oreo.FileMan.Partials
         private void FileBackupPartial_Load(object sender, EventArgs e)
         {
             Watcher.eventHandler += WatcherChangedEvent;
+            Watcher.Start();
+            BackupFileTask();
+
             //读取要备份的文件路径列表
             Task.Factory.StartNew(() =>
             {
@@ -147,10 +156,64 @@ namespace Oreo.FileMan.Partials
         {
             if (Paths.Any(x => e.FullPath.Contains(x.Path)))
             {
-                //FileTool.IsFile(e.FullPath)
-                UIDgvFileAdd(e.Name, e.FullPath, e.ChangeType.ToString());
+                //变动的是文件且文件存在
+                if (FileTool.IsFile(e.FullPath))
+                {
+                    //添加到备份列表
+                    if (!BackupFiles.Contains(e.FullPath)) BackupFiles.Add(e.FullPath);
+                    UIDgvFileAdd(e.Name, e.FullPath, e.ChangeType.ToString());
+                }
             }
         }
+        private void BackupFileTask()
+        {
+            Task.Factory.StartNew(() =>
+            {
+                while (!IsDisposed)
+                {
+                    if (Watcher.IsStart)
+                    {
+                        //获取要备份的文件列表
+                        List<string> temp;
+                        lock (BackupFiles)
+                        {
+                            temp = BackupFiles;
+                            BackupFiles = new List<string>();
+                        }
+
+                        if (ListTool.HasElements(temp))
+                        {
+                            foreach (var t in temp)
+                            {
+                                if (File.Exists(t) && DirTool.Create(FileManBackup))
+                                {
+                                    try
+                                    {
+                                        string filename = Guid.NewGuid().ToString();
+                                        File.Copy(t, DirTool.Combine(FileManBackup, filename), true);
+                                        using (var db = new Muse())
+                                        {
+                                            db.Add(new BackupFiles()
+                                            {
+                                                Name = Path.GetFileName(t),
+                                                Path = Path.GetDirectoryName(t),
+                                                FullPath = t,
+                                                BackupName = filename,
+                                                BackupFullPath = DirTool.Combine(FileManBackup, filename),
+                                                Size = FileTool.Size(t),
+                                                UpdateTime = DateTimeConvert.ToStandardString(DateTime.Now),
+                                            });
+                                        }
+                                    }
+                                    catch (Exception e) { }
+                                }
+                            }
+                        }
+                    }
+                    Thread.Sleep(BACK_UP_INTERVAL);
+                }
+            });
+        }
 
         /// <summary>
         /// 停用或启用所有按钮

+ 38 - 10
Fork.Net/Y.Utils/IOUtils/FileManUtils/FileWatcher.cs

@@ -18,7 +18,7 @@ namespace Y.Utils.IOUtils.FileManUtils
     /// <summary>
     /// 文件更改通知
     /// </summary>
-    public class FileWatcher
+    public class FileWatcher : IDisposable
     {
         /// <summary>
         /// 接受文件监控信息的事件委托
@@ -31,7 +31,7 @@ namespace Y.Utils.IOUtils.FileManUtils
         /// </summary>
         public FileWatcherEventHandler eventHandler;
 
-        private bool _IsStart = false;
+        private bool _IsStart = false, _IsDisposed = false;
         private List<FileSystemWatcher> Watchers = new List<FileSystemWatcher>();
 
         /// <summary>
@@ -54,6 +54,7 @@ namespace Y.Utils.IOUtils.FileManUtils
                     fsw.Changed += ChangedEvent;//更改文件或目录
                     fsw.Deleted += DeletedEvent;//删除文件或目录
                     fsw.Renamed += RenamedEvent;//重命名文件或目录
+                    fsw.Error += ErrorEvent;
                     fsw.IncludeSubdirectories = true;
                     fsw.NotifyFilter = (NotifyFilters)383;
                     Watchers.Add(fsw);
@@ -65,12 +66,15 @@ namespace Y.Utils.IOUtils.FileManUtils
         /// </summary>
         public void Start()
         {
-            _IsStart = true;
-            if (ListTool.HasElements(Watchers))
+            if (!_IsDisposed)
             {
-                foreach (var w in Watchers)
+                _IsStart = true;
+                if (ListTool.HasElements(Watchers))
                 {
-                    w.EnableRaisingEvents = true;
+                    foreach (var w in Watchers)
+                    {
+                        w.EnableRaisingEvents = true;
+                    }
                 }
             }
         }
@@ -79,18 +83,22 @@ namespace Y.Utils.IOUtils.FileManUtils
         /// </summary>
         public void Stop()
         {
-            _IsStart = false;
-            if (ListTool.HasElements(Watchers))
+            if (!_IsDisposed)
             {
-                foreach (var w in Watchers)
+                _IsStart = false;
+                if (ListTool.HasElements(Watchers))
                 {
-                    w.EnableRaisingEvents = false;
+                    foreach (var w in Watchers)
+                    {
+                        w.EnableRaisingEvents = false;
+                    }
                 }
             }
         }
 
 
 
+
         private void DriveMonitor()
         {
             //监测磁盘的插入拔出
@@ -114,5 +122,25 @@ namespace Y.Utils.IOUtils.FileManUtils
         {
             eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), e.OldFullPath, e.OldName));
         }
+        private void ErrorEvent(object sender, ErrorEventArgs e)
+        {
+        }
+
+        public void Dispose()
+        {
+            if (!_IsDisposed)
+            {
+                _IsStart = false;
+                _IsDisposed = true;
+                if (ListTool.HasElements(Watchers))
+                {
+                    foreach (var w in Watchers)
+                    {
+                        w.EnableRaisingEvents = false;
+                        w.Dispose();
+                    }
+                }
+            }
+        }
     }
 }

+ 5 - 2
Fork.Net/Y.Utils/IOUtils/PathUtils/DirTool.cs

@@ -21,10 +21,13 @@ namespace Y.Utils.IOUtils.PathUtils
     public class DirTool
     {
         /// <summary>
-        /// 创建文件目录
+        /// 创建文件目录(文件不存在则创建)
         /// </summary>
         /// <param name="path"></param>
-        /// <returns></returns>
+        /// <returns>
+        /// 如果文件已存在,返回true
+        /// 如果文件不存在,则创建文件,成功返回true,失败返回false
+        /// </returns>
         public static bool Create(string path)
         {
             if (Directory.Exists(path))