在桌面右鍵「新增」或總管「新增」選單中加入 .cs 檔案類型,描述為「C# 程式碼檔案」,預設以記事本開啟。
HKEY_CLASSES_ROOT\.cs:副檔名對應 ProgID(此處設為 cs)。HKEY_CLASSES_ROOT\.cs\ShellNew\NullFile:空值,啟用「新增」選單項目。HKEY_CLASSES_ROOT\cs:ProgID 定義,含友善名稱與預設圖示。HKEY_CLASSES_ROOT\cs\shell\open\command:雙擊/開啟動作的執行命令。using System;
using Microsoft.Win32;
class RegTest {
public static void Main() {
// 1. 副檔名關聯
RegistryKey extKey = Registry.ClassesRoot.CreateSubKey(".cs");
extKey.SetValue("", "cs"); // ProgID
// 2. 啟用「新增」選單
RegistryKey shellNew = extKey.CreateSubKey("ShellNew");
shellNew.SetValue("NullFile", "");
extKey.Close(); shellNew.Close();
// 3. 定義 ProgID
RegistryKey progKey = Registry.ClassesRoot.CreateSubKey("cs");
progKey.SetValue("", "C# Source File");
// 4. 設定預設圖示(使用記事本圖示索引 1)
RegistryKey iconKey = progKey.CreateSubKey("DefaultIcon");
iconKey.SetValue("", @"c:\windows\notepad.exe,1");
iconKey.Close();
// 5. 定義開啟命令
RegistryKey cmdKey = progKey.CreateSubKey(@"shell\open\command");
cmdKey.SetValue("", @"c:\Windows\notepad.exe ""%1""");
cmdKey.Close(); progKey.Close();
Console.WriteLine(".cs file type registered.");
}
}
ClassesRoot 需提權)。notepad.exe 替換為 VS Code、Notepad++ 等編輯器路徑。PerceivedType = text 讓系統識別為文字檔。