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

namespace ryCommon
{
    public class ryStart
    {
        public static bool Use64bit = false;
        /// <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(RegistryHive.LocalMachine, Use64bit ? RegistryView.Registry64: RegistryView.Default);
                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(RegistryHive.LocalMachine, Use64bit ? RegistryView.Registry64 : RegistryView.Default);
                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(RegistryHive.LocalMachine, Use64bit ? RegistryView.Registry64 : RegistryView.Default);
                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(RegistryHive.LocalMachine, Use64bit ? RegistryView.Registry64 : RegistryView.Default);
                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;
            }
        }
        public enum ieMode
        {
            IE7CompatibleView = 7000,
            IE8StandardMode = 8000,
            IE8Forced = 8888
        }
        /// <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(RegistryHive.LocalMachine, Use64bit ? RegistryView.Registry64 : RegistryView.Default);
                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="filename">文件名，要求不带路径</param>
        /// <param name="IEMode">IE内核版本</param>
        /// <returns></returns>
        public static bool SetIE_EMULATION(ieMode IEMode)
        {
            return SetIE_EMULATION(System.IO.Path.GetFileName(Application.ExecutablePath), IEMode);
        }
    }
}
