微软官方帮助文档: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) |
向注册表项中写入值。 |
我们用一个给程序添加操作系统文件后缀关联的功能来演示这些属性和方法应该如何使用。
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) '如果没有创建过,就创建 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 '如果没有找到就一级一级找并保证创建 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 新增的扩展名和设置关联 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); //如果没有创建过,就创建 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) //如果没有找到就一级一级找并保证创建 { 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 新增的扩展名和设置关联 key = keyExtenRoot.OpenSubKey(extendName, true); if (key == null) { key = Registry.ClassesRoot.CreateSubKey(extendName); key.SetValue("", extendDefaultSet); } //MessageBox.Show("关联创建成功!"); return true; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } } |