From 39c2a39fd8d4849abb1ae7c7ec9d4214a9b8bd5b Mon Sep 17 00:00:00 2001 From: jasonchenwork Date: Fri, 20 Jun 2025 13:11:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8F=9C=E5=96=AE=E5=92=8C=E9=85=92=E5=96=AE?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataCheck.cs | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 DataCheck.cs diff --git a/DataCheck.cs b/DataCheck.cs new file mode 100644 index 0000000..da84ae6 --- /dev/null +++ b/DataCheck.cs @@ -0,0 +1,140 @@ +using System.IO; +namespace DataCheck +{ + public class dataCheck + { + public dataCheck() + { + menu_check(); + news_check(); + } + private void menu_check() + { + string menuPath = @"\\SVR01\foods"; + string menuPath_local = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "foods"); + + if (!Directory.Exists(menuPath_local)) + { + Directory.CreateDirectory(menuPath_local); + } + var serverFiles = Directory.GetFiles(menuPath, "*.jpg") + .Select(f => new FileInfo(f)) + .ToDictionary(f => f.Name, f => f); + + var localFiles = Directory.GetFiles(menuPath_local, "*.jpg") + .Select(f => new FileInfo(f)) + .ToDictionary(f => f.Name, f => f); + foreach (var serverFile in serverFiles) + { + bool needsCopy = false; + string localFilePath = Path.Combine(menuPath_local, serverFile.Key); + if (!localFiles.ContainsKey(serverFile.Key)) + { + needsCopy = true; + } + else + { + var localFile = localFiles[serverFile.Key]; + if (serverFile.Value.LastWriteTime > localFile.LastWriteTime) + { + needsCopy = true; + } + } + + if (needsCopy) + { + try + { + File.Copy(serverFile.Value.FullName, localFilePath, true); + Console.WriteLine($"更新菜單: {serverFile.Key}"); + } + catch (Exception ex) + { + Console.WriteLine($"複製菜單失敗 {serverFile.Key}: {ex.Message}"); + } + } + } + // 3-2. 清除本地有但伺服器已經沒有的檔案 + foreach (var localFile in localFiles) + { + if (!serverFiles.ContainsKey(localFile.Key)) + { + try + { + File.Delete(localFile.Value.FullName); + Console.WriteLine($"刪除本地多餘菜單: {localFile.Key}"); + } + catch (Exception ex) + { + Console.WriteLine($"刪除菜單失敗 {localFile.Key}: {ex.Message}"); + } + } + } + } + private void news_check() + { + string newsPath = @"\\SVR01\news"; + string newsPath_local = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "news"); + + if (!Directory.Exists(newsPath_local)) + { + Directory.CreateDirectory(newsPath_local); + } + var serverFiles = Directory.GetFiles(newsPath, "*.jpg") + .Select(f => new FileInfo(f)) + .ToDictionary(f => f.Name, f => f); + + var localFiles = Directory.GetFiles(newsPath_local, "*.jpg") + .Select(f => new FileInfo(f)) + .ToDictionary(f => f.Name, f => f); + foreach (var serverFile in serverFiles) + { + bool needsCopy = false; + string localFilePath = Path.Combine(newsPath_local, serverFile.Key); + if (!localFiles.ContainsKey(serverFile.Key)) + { + needsCopy = true; + } + else + { + var localFile = localFiles[serverFile.Key]; + if (serverFile.Value.LastWriteTime > localFile.LastWriteTime) + { + needsCopy = true; + } + } + + if (needsCopy) + { + try + { + File.Copy(serverFile.Value.FullName, localFilePath, true); + Console.WriteLine($"更新新聞: {serverFile.Key}"); + } + catch (Exception ex) + { + Console.WriteLine($"複製新聞失敗 {serverFile.Key}: {ex.Message}"); + } + } + } + // 3-2. 清除本地有但伺服器已經沒有的檔案 + foreach (var localFile in localFiles) + { + if (!serverFiles.ContainsKey(localFile.Key)) + { + try + { + File.Delete(localFile.Value.FullName); + Console.WriteLine($"刪除本地多餘新聞: {localFile.Key}"); + } + catch (Exception ex) + { + Console.WriteLine($"刪除新聞失敗 {localFile.Key}: {ex.Message}"); + } + } + } + + } + + } +} \ No newline at end of file