改成以 TASK 多工處理 或 計算大小 以下是基於 **`Task`** 的改進版程式,針對資料夾檔案與資料夾大小計算,並透過非同步多工處理方式提升效能。 同時仍支持導出 JSON 格式。 --- ### **程式碼範例** #### **1. JSON 導出邏輯改進** 非同步多工處理子資料夾大小計算與檔案遍歷,提供更高效的操作。 ```csharp using System; using System.Collections.Generic; using System.IO; using System.Text.Json; using System.Threading.Tasks; public class FolderToJsonHandler { public class FileSystemItem { public string Name { get; set; } public string Type { get; set; } // File 或 Folder public long Size { get; set; } // Bytes public DateTime CreationTime { get; set; } public DateTime ModificationTime { get; set; } } public async Task ExportFolderToJsonAsync(string folderPath, string outputPath) { if (!Directory.Exists(folderPath)) { Console.WriteLine("指定的資料夾不存在。"); return; } var items = new List(); await TraverseFolderAsync(folderPath, items); var options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(items, options); await File.WriteAllTextAsync(outputPath, json, System.Text.Encoding.UTF8); Console.WriteLine($"資料已成功導出為 JSON,存於: {outputPath}"); } private async Task TraverseFolderAsync(string folderPath, List items) { var folderInfo = new DirectoryInfo(folderPath); // 使用 Task 計算資料夾大小 long folderSize = await CalculateFolderSizeAsync(folderInfo); // 新增資料夾資訊到列表 items.Add(new FileSystemItem { Name = folderInfo.Name, Type = "Folder", Size = folderSize, CreationTime = folderInfo.CreationTime, ModificationTime = folderInfo.LastWriteTime }); // 處理檔案 foreach (var file in folderInfo.GetFiles()) { items.Add(new FileSystemItem { Name = file.Name, Type = "File", Size = file.Length, CreationTime = file.CreationTime, ModificationTime = file.LastWriteTime }); } // 處理子資料夾(非同步執行) var tasks = new List(); foreach (var subFolder in folderInfo.GetDirectories()) { tasks.Add(TraverseFolderAsync(subFolder.FullName, items)); } await Task.WhenAll(tasks); // 等待所有子資料夾處理完成 } private async Task CalculateFolderSizeAsync(DirectoryInfo folderInfo) { long size = 0; // 計算檔案大小 foreach (var file in folderInfo.GetFiles()) { size += file.Length; } // 遞迴計算子資料夾大小 var tasks = new List>(); foreach (var subFolder in folderInfo.GetDirectories()) { tasks.Add(CalculateFolderSizeAsync(subFolder)); } var results = await Task.WhenAll(tasks); foreach (var result in results) { size += result; } return size; } } ``` --- #### **2. 主程式** 用非同步方式導出所選的資料夾,並生成 JSON。 ```csharp public class Program { public static async Task Main(string[] args) { if (args.Length > 0) { string folderPath = args[0]; string outputPath = Path.Combine(folderPath, "FolderExport.json"); var handler = new FolderToJsonHandler(); await handler.ExportFolderToJsonAsync(folderPath, outputPath); // 非同步導出 JSON } else { Console.WriteLine("請選擇一個資料夾以執行操作。"); } } } ``` --- ### **程式改進點** 1. **多工處理:** - 使用 `Task` 和 `async/await` 提升效能。 - 對子資料夾和檔案操作進行非同步處理,減少等待時間。 2. **資料夾大小計算:** - 非同步遞迴計算資料夾大小,避免阻塞主執行緒。 3. **導出格式:** - 保持原有 JSON 導出格式,支持資料夾屬性和檔案屬性。 --- ### **範例 JSON 輸出** 執行後生成的 JSON 文件如下: ```json [ { "Name": "Documents", "Type": "Folder", "Size": 102400, "CreationTime": "2025-04-30T12:00:00", "ModificationTime": "2025-04-30T12:00:00" }, { "Name": "file.txt", "Type": "File", "Size": 2048, "CreationTime": "2025-04-29T11:00:00", "ModificationTime": "2025-04-29T11:00:00" } ] ``` --- ### **注意事項** - **效能提升**:對於含有大量子資料夾的資料夾結構,多工處理能有效減少處理時間。 - **權限問題**:執行程式可能需要適當的檔案讀寫權限。 - **檔案大小計算**:計算資料夾大小可能需要一些時間,尤其是資料夾內含多個檔案時。 如果需要更進一步擴展功能或自訂邏輯,請隨時告訴我! 😊