給我 C# .Net 8.0 的 Class, 可在 Windows 檔案總管的 ContextMenu 中回呼 Class 中的函數, 函數的功能可將選到的檔案或資料夾, 包含所有的子資料夾, 以 Json 格式寫出每個項目的屬性, 屬性包括資料夾名稱, 資料夾大小, 檔案名稱, 檔案大小, 建立時間, 修改時間. 以下是 C# .NET 8.0 的完整範例,展示如何在 Windows 檔案總管的 Context Menu 中加入自定義功能, 回呼程式中的函數,將選中的檔案或資料夾及其子資料夾內容以 JSON 格式輸出。 --- ### **程式碼範例** #### **1. Context Menu 註冊** 先設置 Windows Registry 來添加 Context Menu,並呼叫指定程式。 ```csharp using Microsoft.Win32; public static class ContextMenuRegister { public static void RegisterContextMenu() { string keyPath = @"Software\Classes\Folder\shell\ExportToJson"; string commandPath = @"Software\Classes\Folder\shell\ExportToJson\command"; // 創建主選單項目 using (var key = Registry.CurrentUser.CreateSubKey(keyPath)) { if (key != null) { key.SetValue("", "Export Folder to JSON"); // 右鍵選單顯示的名稱 } } // 創建執行命令 using (var key = Registry.CurrentUser.CreateSubKey(commandPath)) { if (key != null) { string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location; key.SetValue("", $"\"{exePath}\" \"%1\""); // 傳入所選資料夾路徑 } } Console.WriteLine("Context Menu 已成功註冊。"); } public static void UnregisterContextMenu() { string keyPath = @"Software\Classes\Folder\shell\ExportToJson"; Registry.CurrentUser.DeleteSubKeyTree(keyPath, false); Console.WriteLine("Context Menu 已取消註冊。"); } } ``` --- #### **2. JSON 導出邏輯** 處理所選檔案或資料夾,並將屬性導出為 JSON。 ```csharp using System; using System.Collections.Generic; using System.IO; using System.Text.Json; 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 void ExportFolderToJson(string folderPath, string outputPath) { if (!Directory.Exists(folderPath)) { Console.WriteLine("指定的資料夾不存在。"); return; } var items = new List(); TraverseFolder(folderPath, items); var options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(items, options); File.WriteAllText(outputPath, json, System.Text.Encoding.UTF8); Console.WriteLine($"資料已成功導出為 JSON,存於: {outputPath}"); } private void TraverseFolder(string folderPath, List items) { // 處理資料夾屬性 var folderInfo = new DirectoryInfo(folderPath); items.Add(new FileSystemItem { Name = folderInfo.Name, Type = "Folder", Size = CalculateFolderSize(folderInfo), 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 }); } // 遞迴子資料夾 foreach (var subFolder in folderInfo.GetDirectories()) { TraverseFolder(subFolder.FullName, items); } } private long CalculateFolderSize(DirectoryInfo folderInfo) { long size = 0; // 計算檔案大小 foreach (var file in folderInfo.GetFiles()) { size += file.Length; } // 計算子資料夾大小 foreach (var subFolder in folderInfo.GetDirectories()) { size += CalculateFolderSize(subFolder); } return size; } } ``` --- #### **3. 主程式** 將選擇的資料夾路徑作為參數傳入,並執行導出邏輯。 ```csharp public class Program { public static void Main(string[] args) { if (args.Length > 0) { string folderPath = args[0]; string outputPath = Path.Combine(folderPath, "FolderExport.json"); var handler = new FolderToJsonHandler(); handler.ExportFolderToJson(folderPath, outputPath); } else { Console.WriteLine("請選擇一個資料夾以執行操作。"); } } } ``` --- ### **功能細節** 1. **Registry 註冊**: - 將 Context Menu 加入資料夾的右鍵選單。 - 使用 `%1` 參數傳遞所選資料夾路徑。 2. **資料屬性導出**: - 支援資料夾名稱、大小、檔案名稱、大小、建立時間和修改時間。 - 遞迴處理所有子資料夾。 3. **導出格式**: - 將所有屬性導出為 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" } ] ``` --- ### **注意事項** - **權限**:需要以管理員權限執行註冊或取消註冊。 - **檔案大小計算**:對於大型資料夾,計算大小可能需要一些時間。 - **JSON 存放路徑**:預設存於選定資料夾內,檔名為 `FolderExport.json`。 這樣的功能應能滿足你的需求。如果還需要其他擴展,隨時告訴我! 😊