using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace ryCommon
{ 
    /// <summary>
    /// 设置注册表操作,部分功能需要管理员权限
    /// </summary>
    public class RyRegedit
    {
        /// <summary>
        /// 访问的注册表位置(64位还是32位)
        /// </summary>
        public static RegistryView UseSystemBit = RegistryView.Default;
        /// <summary>
        /// 访问的注册表节点
        /// </summary>
        public static RegistryHive RegRoot = RegistryHive.LocalMachine;
        /// <summary>
        /// 设置是否开机启动
        /// </summary>
        /// <param name="AutoRun">是否开机启动</param>
        /// <param name="StartName">开机启动名称</param>
        /// <param name="StartCommand">启动命令</param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 设置是否开机启动
        /// </summary>
        /// <param name="AutoRun">是否开机启动</param>
        /// <param name="StartName">开机启动名称</param>
        /// <returns></returns>
        public static bool SetAutoRun(bool AutoRun, string StartName)
        {
            return SetAutoRun(AutoRun, StartName, "\"" + Application.ExecutablePath + "\" q");
        }
        /// <summary>
        /// 把指定文件设置为开机启动或取消开机启动
        /// </summary>
        /// <param name="AutoRun">是否开机启动</param>
        /// <param name="StartName">开机启动名称</param>
        /// <param name="Path">要开机启动的文件路径</param>
        /// <returns></returns>
        public static bool SetAutoRunByPath(bool AutoRun, string StartName, string Path)
        {
            return SetAutoRun(AutoRun, StartName, "\"" + Path + "\" q");
        }
        /// <summary>
        /// 检查是否开机启动
        /// </summary>
        /// <param name="StartName">开机启动名称</param>
        /// <param name="StartCommand">开机启动命令</param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 检查是否开机启动
        /// </summary>
        /// <param name="StartName">开机启动名称</param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 判断指定文件是否是开机启动
        /// </summary>
        /// <param name="StartName">开机启动名称</param>
        /// <param name="Path">文件路径</param>
        /// <returns></returns>
        public static bool IsAutoRunByPath(string StartName, string Path)
        {
            return IsAutoRun(StartName, "\"" + Path + "\" q");
        }
        /// <summary>
        /// 判断当前程序是否是开机启动
        /// </summary>
        /// <param name="StartName">开机启动名称</param>
        /// <returns></returns>
        public static bool IsAutoRunByMe(string StartName)
        {
            return IsAutoRun(StartName, "\"" + Application.ExecutablePath + "\" q");
        }
        /// <summary>
        /// 判断当前程序是否是开机启动
        /// </summary>
        /// <param name="StartName">开机启动名称</param>
        /// <param name="Cmd">启动命令行</param>
        /// <returns></returns>
        public static bool IsAutoRunByMe(string StartName, string Cmd)
        {
            return IsAutoRun(StartName, "\"" + Application.ExecutablePath + "\" " + Cmd);
        }
        /// <summary>
        /// 设置指定文件的浏览器控件内核版本
        /// </summary>
        /// <param name="filename">文件名，要求不带路径</param>
        /// <param name="IEMode">7000 表示IE7兼容视图模式;8000 表示IE8 标准模式 ;8888 表示IE8 强制标准模式，在渲染失败的情况下不尝试用兼容视图模式</param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 设置文件关联
        /// </summary>
        /// <param name="Ext">扩展名(如.apk)</param>
        /// <param name="ExtName">映射的扩展名(如apkfile)</param>
        /// <param name="sName">app名称(建议用英文数字)</param>
        /// <param name="AssDes">文件关联描述</param>
        /// <param name="filePath">文件路径</param>
        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);
        }
        /// <summary>
        /// 设置文件关联
        /// </summary>
        /// <param name="ExtName">映射的扩展名(如apkfile)</param>
        /// <param name="sName">app名称(建议用英文数字)</param>
        /// <param name="AssDes">文件关联描述</param>
        /// <param name="filePath">文件路径</param>
        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();
        }
        /// <summary>
        /// 在指定文件格式右键菜单中增加菜单
        /// </summary>
        ///<param name="Ext">扩展名(如.apk)</param>
        /// <param name="sName">app名称(建议用英文数字)</param>
        /// <param name="AssDes">文件关联描述</param>
        /// <param name="filePath">文件路径</param>
        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();
        }
        /// <summary>
        /// 删除文件关联
        /// </summary>
        /// <param name="ExtName">映射的扩展名(如apkfile)</param>
        /// <param name="sName">app名称(建议用英文数字)</param>
        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();
        }
        /// <summary>
        /// 获取文件关联是否存在
        /// </summary>
        /// <param name="ExtName">映射的扩展名(如apkfile)</param>
        /// <param name="sName">app名称(建议用英文数字)</param>
        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;
        }
        /// <summary>
        /// 采用的IE模式
        /// </summary>
        public enum IeMode
        {
            /// <summary>
            /// IE7兼容视图
            /// </summary>
            IE7CompatibleView = 7000,
            /// <summary>
            /// IE8 标准模式
            /// </summary>
            IE8StandardMode = 8000,
            /// <summary>
            /// IE8 强制标准模式，在渲染失败的情况下不尝试用兼容视图模式
            /// </summary>
            IE8Forced = 8888,
            /// <summary>
            /// IE9 标准模式
            /// </summary>
            IE9StandardMode = 9000,
            /// <summary>
            /// IE9 强制标准模式，在渲染失败的情况下不尝试用兼容视图模式
            /// </summary>
            IE9Forced = 9999,
            /// <summary>
            /// IE10 标准模式
            /// </summary>
            IE10StandardMode = 10000,
            /// <summary>
            /// IE10 强制标准模式，在渲染失败的情况下不尝试用兼容视图模式
            /// </summary>
            IE10Forced = 10001
        }
        /// <summary>
        /// 设置当前软件的浏览器控件内核版本
        /// </summary>
        /// <param name="IEMode">7000 表示IE7兼容视图模式;8000 表示IE8 标准模式 ;8888 表示IE8 强制标准模式，在渲染失败的情况下不尝试用兼容视图模式</param>
        /// <returns></returns>
        public static bool SetIE_EMULATION(uint IEMode)
        {
            return SetIE_EMULATION(System.IO.Path.GetFileName(Application.ExecutablePath), IEMode);
        }
        /// <summary>
        /// 设置指定文件的浏览器控件内核版本
        /// </summary>
        /// <param name="filename">文件名，要求不带路径</param>
        /// <param name="IEMode">IE内核版本</param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 设置当前软件的浏览器控件内核版本
        /// </summary>
        /// <param name="IEMode">IE内核版本</param>
        /// <returns></returns>
        public static bool SetIE_EMULATION(IeMode IEMode)
        {
            return SetIE_EMULATION(System.IO.Path.GetFileName(Application.ExecutablePath), IEMode);
        }
    }
}
