using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using System.Windows.Forms; namespace ryCommon { /// /// 设置注册表操作,部分功能需要管理员权限 /// public class RyRegedit { /// /// 访问的注册表位置(64位还是32位) /// public static RegistryView UseSystemBit = RegistryView.Default; /// /// 访问的注册表节点 /// public static RegistryHive RegRoot = RegistryHive.LocalMachine; /// /// 设置是否开机启动 /// /// 是否开机启动 /// 开机启动名称 /// 启动命令 /// public static bool SetAutoRun(bool AutoRun, string StartName, string StartCommand) { try { RegistryKey LMach = RegistryKey.OpenBaseKey(RegRoot, UseSystemBit); RegistryKey softwareRun = LMach.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); if (AutoRun) { softwareRun.SetValue(StartName, StartCommand, RegistryValueKind.String); } else { if (softwareRun.GetValue(StartName) != null) { softwareRun.DeleteValue(StartName); } } softwareRun.Close(); LMach.Close(); return true; } catch { return false; } } /// /// 设置是否开机启动 /// /// 是否开机启动 /// 开机启动名称 /// public static bool SetAutoRun(bool AutoRun, string StartName) { return SetAutoRun(AutoRun, StartName, "\"" + Application.ExecutablePath + "\" q"); } /// /// 把指定文件设置为开机启动或取消开机启动 /// /// 是否开机启动 /// 开机启动名称 /// 要开机启动的文件路径 /// public static bool SetAutoRunByPath(bool AutoRun, string StartName, string Path) { return SetAutoRun(AutoRun, StartName, "\"" + Path + "\" q"); } /// /// 检查是否开机启动 /// /// 开机启动名称 /// 开机启动命令 /// public static bool IsAutoRun(string StartName, string StartCommand) { try { bool sxResult = false; RegistryKey LMach = RegistryKey.OpenBaseKey(RegRoot, UseSystemBit); RegistryKey softwareRun = LMach.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); if (softwareRun.GetValue(StartName, "").ToString() == StartCommand) { sxResult = true; } else { sxResult = false; } softwareRun.Close(); LMach.Close(); return sxResult; } catch { return false; } } /// /// 检查是否开机启动 /// /// 开机启动名称 /// public static bool IsAutoRun(string StartName) { try { bool sxResult = false; RegistryKey LMach = RegistryKey.OpenBaseKey(RegRoot, UseSystemBit); RegistryKey softwareRun = LMach.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); if (softwareRun.GetValue(StartName, "").ToString() != "") { sxResult = true; } else { sxResult = false; } softwareRun.Close(); LMach.Close(); return sxResult; } catch { return false; } } /// /// 判断指定文件是否是开机启动 /// /// 开机启动名称 /// 文件路径 /// public static bool IsAutoRunByPath(string StartName, string Path) { return IsAutoRun(StartName, "\"" + Path + "\" q"); } /// /// 判断当前程序是否是开机启动 /// /// 开机启动名称 /// public static bool IsAutoRunByMe(string StartName) { return IsAutoRun(StartName, "\"" + Application.ExecutablePath + "\" q"); } /// /// 判断当前程序是否是开机启动 /// /// 开机启动名称 /// 启动命令行 /// public static bool IsAutoRunByMe(string StartName, string Cmd) { return IsAutoRun(StartName, "\"" + Application.ExecutablePath + "\" " + Cmd); } /// /// 设置指定文件的浏览器控件内核版本 /// /// 文件名,要求不带路径 /// 7000 表示IE7兼容视图模式;8000 表示IE8 标准模式 ;8888 表示IE8 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式 /// public static bool SetIE_EMULATION(string filename, uint IEMode) { try { RegistryKey LMach = RegistryKey.OpenBaseKey(RegRoot, UseSystemBit); RegistryKey softwareRun = LMach.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true); softwareRun.SetValue(filename, IEMode, RegistryValueKind.DWord); softwareRun.Close(); LMach.Close(); return true; } catch { return false; } } /// /// 设置文件关联 /// /// 扩展名(如.apk) /// 映射的扩展名(如apkfile) /// app名称(建议用英文数字) /// 文件关联描述 /// 文件路径 public static void SetFileAssociation(string Ext, string ExtName, string sName, string AssDes, string filePath) { RegistryKey LRoot = Registry.ClassesRoot; try { RegistryKey reg1 = LRoot.CreateSubKey(Ext); reg1.SetValue("", ExtName); reg1.Close(); } catch { } LRoot.Close(); SetFileAssociation(ExtName, sName, AssDes, filePath); } /// /// 设置文件关联 /// /// 映射的扩展名(如apkfile) /// app名称(建议用英文数字) /// 文件关联描述 /// 文件路径 public static void SetFileAssociation(string ExtName, string sName, string AssDes, string filePath) { RegistryKey LRoot = Registry.ClassesRoot; try { SetReg(sName); SetReg("open"); RegistryKey reg_3 = LRoot.CreateSubKey(ExtName + @"\DefaultIcon"); reg_3.SetValue("", filePath + ",0"); reg_3.Close(); void SetReg(string key) { RegistryKey reg1 = LRoot.CreateSubKey(ExtName + @"\shell\" + key); reg1.SetValue("", AssDes); reg1.Close(); RegistryKey reg_2 = LRoot.CreateSubKey(ExtName + @"\shell\" + key + "\\command"); reg_2.SetValue("", "\"" + filePath + "\" \"%1\""); reg_2.Close(); } } catch { } LRoot.Close(); } /// /// 在指定文件格式右键菜单中增加菜单 /// ///扩展名(如.apk) /// app名称(建议用英文数字) /// 文件关联描述 /// 文件路径 public static void SetFileAssociationOpenAs(string Ext, string sName, string AssDes, string filePath) { RegistryKey LRoot = Registry.ClassesRoot; try { SetReg(sName); void SetReg(string key) { RegistryKey reg1 = LRoot.CreateSubKey(Ext + @"\shell\" + key); reg1.SetValue("", AssDes); reg1.Close(); RegistryKey reg_2 = LRoot.CreateSubKey(Ext + @"\shell\" + key + "\\command"); reg_2.SetValue("", "\"" + filePath + "\" \"%1\""); reg_2.Close(); } } catch { } LRoot.Close(); } /// /// 删除文件关联 /// /// 映射的扩展名(如apkfile) /// app名称(建议用英文数字) public static void DelFileAssociation(string ExtName, string sName) { RegistryKey LRoot = Registry.ClassesRoot; try { RegistryKey reg1 = LRoot.OpenSubKey(ExtName + @"\shell"); if (reg1 != null) { reg1.DeleteSubKeyTree(sName); reg1.Close(); } } catch { } LRoot.Close(); } /// /// 获取文件关联是否存在 /// /// 映射的扩展名(如apkfile) /// app名称(建议用英文数字) public static bool GetFileAssociation(string ExtName, string sName) { bool result = false; RegistryKey LRoot = Registry.ClassesRoot; try { RegistryKey reg1 = LRoot.OpenSubKey(ExtName + @"\shell\"+ sName); if (reg1 != null) { reg1.Close(); result = true; } } catch { } LRoot.Close(); return result; } /// /// 采用的IE模式 /// public enum IeMode { /// /// IE7兼容视图 /// IE7CompatibleView = 7000, /// /// IE8 标准模式 /// IE8StandardMode = 8000, /// /// IE8 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式 /// IE8Forced = 8888, /// /// IE9 标准模式 /// IE9StandardMode = 9000, /// /// IE9 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式 /// IE9Forced = 9999, /// /// IE10 标准模式 /// IE10StandardMode = 10000, /// /// IE10 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式 /// IE10Forced = 10001 } /// /// 设置当前软件的浏览器控件内核版本 /// /// 7000 表示IE7兼容视图模式;8000 表示IE8 标准模式 ;8888 表示IE8 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式 /// public static bool SetIE_EMULATION(uint IEMode) { return SetIE_EMULATION(System.IO.Path.GetFileName(Application.ExecutablePath), IEMode); } /// /// 设置指定文件的浏览器控件内核版本 /// /// 文件名,要求不带路径 /// IE内核版本 /// public static bool SetIE_EMULATION(string filename, IeMode IEMode) { try { RegistryKey LMach = RegistryKey.OpenBaseKey(RegRoot, UseSystemBit); RegistryKey softwareRun = LMach.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true); softwareRun.SetValue(filename, IEMode, RegistryValueKind.DWord); softwareRun.Close(); LMach.Close(); return true; } catch { return false; } } /// /// 设置当前软件的浏览器控件内核版本 /// /// IE内核版本 /// public static bool SetIE_EMULATION(IeMode IEMode) { return SetIE_EMULATION(System.IO.Path.GetFileName(Application.ExecutablePath), IEMode); } } }