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; } } /// /// 采用的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); } } }