微軟官方幫助文檔:RegistryProxy 類 (Microsoft.VisualBasic.MyServices) | Microsoft Docs
屬性
ClassesRoot |
返回 RegistryKey 類型,該類型提供對 HKEY_CLASSES_ROOT 的訪問。 |
CurrentConfig |
返回 RegistryKey 類型,該類型提供對 HKEY_CURRENT_CONFIG 的訪問。 |
CurrentUser |
返回 RegistryKey 類型,該類型提供對 HKEY_CURRENT_USER 的訪問。 |
LocalMachine |
返回 RegistryKey 類型,該類型提供對 HKEY_LOCAL_MACHINE 的訪問。 |
PerformanceData |
返回 RegistryKey 類型,該類型提供對 HKEY_PERFORMANCE_DATA 的訪問。 |
Users | 返回 RegistryKey 類型,該類型提供對 HKEY_USERS 的訪問。 |
方法
GetValue(String, String, Object) |
從注冊表項中獲取值。 |
SetValue(String, String, Object) |
向注冊表項中寫入值。 |
SetValue(String, String, Object,
RegistryValueKind) |
向注冊表項中寫入值。 |
我們用一個給程序添加操作系統(tǒng)文件后綴關聯(lián)的功能來演示這些屬性和方法應該如何使用。
Vb.Net |
Public Shared Function SetFileExtend(ByVal extendName As String, ByVal appPath As String, ByVal directions As String) As Boolean Try Dim extendDefaultSet As String = extendName.Trim(".").ToUpper() Dim key As RegistryKey Dim keyExtenRoot As RegistryKey = Registry.ClassesRoot.OpenSubKey(extendDefaultSet, True) '如果沒有創(chuàng)建過,就創(chuàng)建 If keyExtenRoot Is Nothing Then '1 設置自定義文件的雙擊打開 keyExtenRoot = Registry.ClassesRoot.CreateSubKey(extendDefaultSet) keyExtenRoot.SetValue("", directions) key = keyExtenRoot.CreateSubKey("shell") key = key.CreateSubKey("open") key = key.CreateSubKey("command") Else key = keyExtenRoot.OpenSubKey("shell")?.OpenSubKey("open")?.OpenSubKey("command", True) If key Is Nothing Then '如果沒有找到就一級一級找并保證創(chuàng)建 keyExtenRoot.SetValue("", directions) key = keyExtenRoot.OpenSubKey("shell", True) If key Is Nothing Then key = keyExtenRoot.CreateSubKey("shell") End If keyExtenRoot = key key = key.OpenSubKey("open", True) If key Is Nothing Then key = keyExtenRoot.CreateSubKey("open") End If keyExtenRoot = key key = key.OpenSubKey("command", True) If key Is Nothing Then key = key.CreateSubKey("command") End If End If End If key.SetValue("", String.Format("{0} %1", appPath)) '2 設置自定義文件的默認圖標 --這點測試時有點問題,還有待研究 key = keyExtenRoot.OpenSubKey("DefaultIcon", True) If key Is Nothing Then key = keyExtenRoot.CreateSubKey("DefaultIcon") End If Dim strIcoPath As String = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath.Replace("/", "\")), "SanMuGrid32_32.ico") key.SetValue("", strIcoPath) '3 新增的擴展名和設置關聯(lián) key = keyExtenRoot.OpenSubKey(extendName, True) If key Is Nothing Then key = Registry.ClassesRoot.CreateSubKey(extendName) key.SetValue("", extendDefaultSet) End If Return True Catch ex As Exception MessageBox.Show(ex.Message) Return False End Try End Function |
C# |
public static bool SetFileExtend(string extendName, string appPath, string directions) { try { string extendDefaultSet = extendName.Trim(".").ToUpper(); RegistryKey key; RegistryKey keyExtenRoot= Registry.ClassesRoot.OpenSubKey(extendDefaultSet, true); //如果沒有創(chuàng)建過,就創(chuàng)建 if (keyExtenRoot == null) { //1 設置自定義文件的雙擊打開 keyExtenRoot = Registry.ClassesRoot.CreateSubKey(extendDefaultSet); keyExtenRoot.SetValue("", directions); key = keyExtenRoot.CreateSubKey("shell"); key = key.CreateSubKey("open"); key = key.CreateSubKey("command"); } else { key = keyExtenRoot.OpenSubKey("shell")?.OpenSubKey("open")?.OpenSubKey("command", true); if (key == null) //如果沒有找到就一級一級找并保證創(chuàng)建 { keyExtenRoot.SetValue("", directions); key = keyExtenRoot.OpenSubKey("shell",true); if (key == null) { key = keyExtenRoot.CreateSubKey("shell"); } keyExtenRoot = key; key = key.OpenSubKey("open", true); if (key == null) { key = keyExtenRoot.CreateSubKey("open"); } keyExtenRoot = key; key = key.OpenSubKey("command",true); if (key == null) { key = key.CreateSubKey("command"); } } } key.SetValue("", string.Format("{0} %1", appPath)); //2 設置自定義文件的默認圖標 --這點測試時有點問題,還有待研究 key =keyExtenRoot.OpenSubKey("DefaultIcon", true); if (key == null) { key = keyExtenRoot.CreateSubKey("DefaultIcon"); } string strIcoPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath.Replace("/", "\\")), "SanMuGrid32_32.ico"); key.SetValue("", strIcoPath); //3 新增的擴展名和設置關聯(lián) key = keyExtenRoot.OpenSubKey(extendName, true); if (key == null) { key = Registry.ClassesRoot.CreateSubKey(extendName); key.SetValue("", extendDefaultSet); } //MessageBox.Show("關聯(lián)創(chuàng)建成功!"); return true; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } } |