C# 安装软件,启动程序,判断系统版本,更改IE注册表及内核,注册程序

namespace Init
{
    public class Comm
    {
        /// <summary>
        /// 判断系统版本
        /// </summary>
        /// <param name="isWin7"></param>
        /// <param name="isWinXP"></param>
        /// <param name="isWin10"></param>
        /// <returns></returns>
        public static string CheckOS(ref bool isWin7, ref bool isWinXP, ref bool isWin10)
        {
            string osVersion = "";
            OperatingSystem os = Environment.OSVersion;
            switch (os.Platform)
            {
                case PlatformID.Win32Windows:
                    switch (os.Version.Minor)
                    {
                        case 0:
                            osVersion = "Windows 95 ";
                            break;
                        case 10:
                            if (os.Version.Revision.ToString() == "2222A ")
                                osVersion = "Windows 98 第二版 ";
                            else
                                osVersion = "Windows 98 ";
                            break;
                        case 90:
                            osVersion = "Windows Me ";
                            break;
                    }
                    break;
                case PlatformID.Win32NT:
                    switch (os.Version.Major)
                    {
                        case 3:
                            osVersion = "Windows NT 3.51 ";
                            break;
                        case 4:
                            osVersion = "Windows NT 4.0 ";
                            break;
                        case 5:
                            switch (os.Version.Minor)
                            {
                                case 0:
                                    osVersion = "Windows 200 ";
                                    break;
                                case 1:
                                    osVersion = "Windows XP ";
                                    isWinXP = true;
                                    break;
                                case 2:
                                    osVersion = "Windows 2003 ";
                                    break;
                            }
                            break;
                        case 6:
                            switch (os.Version.Minor)
                            {
                                case 0:
                                    osVersion = "Windows Vista ";
                                    break;
                                case 1:
                                    osVersion = "Windows 7 ";
                                    isWin7 = true;
                                    break;
                                case 2:

                                    osVersion = "Windows 8或以上 ";
                                    isWin10 = true;
                                    break;
                            }
                            break;
                    }
                    break;
            }

            return osVersion + "[" + os.Version + "]";
        }

        /// <summary>
        /// 安装驱动软件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="arg"></param>
        public static void DriverSetup(string fileName, string arg)
        {
            ProcessStartInfo dispinfo = new ProcessStartInfo();
            dispinfo.FileName = fileName;
            dispinfo.Arguments = arg;
            dispinfo.WindowStyle = ProcessWindowStyle.Normal;
            Process dispro = Process.Start(dispinfo);
            dispro.WaitForExit();
        }

        /// <summary>
        /// 启动服务/程序
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="args"></param>
        public static void Driver(string filename, string args)
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = filename;
            psi.Arguments = args;
            psi.WindowStyle = ProcessWindowStyle.Normal;
            Process proc = Process.Start(psi);
        }

        /// <summary>
        /// 手动注册控件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="arg">执行语句</param>
        public static void ProcessReg(string fileName, string arg)
        {
            Process p_reg = new Process();

            p_reg.StartInfo.FileName = fileName;

            p_reg.StartInfo.Arguments = arg;

            p_reg.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            if (p_reg.Start() == true)
            {
                p_reg.Dispose();
            }
        }

        /// <summary>
        /// 设置开机自启
        /// </summary>
        /// <param name="started"></param>
        /// <param name="exeName"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool runWhenStart(bool started, string exeName, string path)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);//打开注册表子项
            if (key == null)//如果该项不存在的话,则创建该子项
            {
                key = Registry.LocalMachine.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
            }
            if (started == true)
            {
                try
                {
                    key.SetValue(exeName, path);//设置为开机启动
                    key.Close();
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                try
                {
                    key.DeleteValue(exeName);//取消开机启动
                    key.Close();
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 日志部分
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="type"></param>
        /// <param name="content"></param>
        public static void WriteLogs(string content)
        {
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                if (!string.IsNullOrEmpty(path))
                {
                    path = AppDomain.CurrentDomain.BaseDirectory + "log";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "\\" + "log.txt";
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);
                        fs.Close();
                    }
                    if (File.Exists(path))
                    {
                        StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff  #") + content);
                        sw.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Comm.WriteLogs("\r\nError 1003:" + ex.ToString() + "\r\n");
                Comm.WriteLogs("-" + ex.StackTrace + "\r\n");
            }
        }

        /// <summary>
        /// 打印指定文件的详细信息
        /// </summary>
        /// <param name="path">指定文件的路径</param>
        public static double GetFileSize(string path)
        {
            double size = 0;
            System.IO.FileInfo fileInfo = null;
            try
            {
                fileInfo = new System.IO.FileInfo(path);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                // 其他处理异常的代码
            }
            // 如果文件存在
            if (fileInfo != null && fileInfo.Exists)
            {
                size = System.Math.Ceiling(fileInfo.Length / 1024.0);
            }

            return size;
        }

        /// <summary>
        /// 获取数字列表里从小到大排列最先却少的那个数字
        /// <param name="numbers">已经使用的数字列表</param>
        /// <returns></returns>
        public static string findMinNumber(List<string> numbers)
        {
            for (int i = 1; i <= numbers.Count; i++)
            {
                if (numbers.IndexOf(i.ToString()) == -1) return i.ToString();
            }
            return (numbers.Count + 1).ToString();
        }
    }
    public class SetIERegedit
    {
        /// <summary>
        /// 设置IE对应的注册表,勾选 允许活动内容在"我的电脑"的文件中运行
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetLockDown(string key, string value)
        {
            RegistryKey rk = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\Main", true);

            RegistryKey rkfc = rk.OpenSubKey("FeatureControl", true);

            if (rkfc == null)//没有FeatureControl
            {
                rk.CreateSubKey("FeatureControl");
                rkfc = rk.OpenSubKey("FeatureControl", true);
            }


            RegistryKey rkfcfll = rkfc.OpenSubKey("FEATURE_LOCALMACHINE_LOCKDOWN", true);

            if (rkfcfll == null)//没有FEATURE_LOCALMACHINE_LOCKDOWN
            {
                rkfc.CreateSubKey("FEATURE_LOCALMACHINE_LOCKDOWN");
                rkfcfll = rkfc.OpenSubKey("FEATURE_LOCALMACHINE_LOCKDOWN", true);
            }

            rkfcfll.SetValue(key, value, RegistryValueKind.DWord);

            Comm.WriteLogs("IE-Regedit-LOCKDOWN-" + key + "-" + value + "\r\n");

        }
        /// <summary>
        /// /设置IE对应的ActiveX注册表
        /// </summary>
        /// <param name="Flag">1-本地Intranet 2-受信任站点 3-Internet</param>
        public static void EditIEActiveX(int Flag)
        {

            //ActiveX 控件和插件:下载已签署的 ActiveX 控件 
            Comm.WriteLogs("IE-Regedit-Zones-1-2104-0" + "\r\n");
            AddOrEditKeyValue("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\" + Flag, "1001", "0");// 3=禁用、0=启用、1=提示  

            //ActiveX 控件和插件:下载未签署的 ActiveX 控件 
            Comm.WriteLogs("IE-Regedit-Zones-1-2104-0" + "\r\n");
            AddOrEditKeyValue("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\" + Flag, "1004", "0");

            //ActiveX 控件和插件:运行 ActiveX 控件 
            Comm.WriteLogs("IE-Regedit-Zones-1-2104-0" + "\r\n");
            AddOrEditKeyValue("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\" + Flag, "1200", "0");

            //对没有标记为可安全执行脚本的 ActiveX 控件进行初始化和脚本运行 
            Comm.WriteLogs("IE-Regedit-Zones-1-2104-0" + "\r\n");
            AddOrEditKeyValue("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\" + Flag, "1201", "0");

            // ActiveX 控件和插件:允许以前未使用的 ActiveX 控件在没有提示的情况下运行 ^  
            Comm.WriteLogs("IE-Regedit-Zones-1-2104-0" + "\r\n");
            AddOrEditKeyValue("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\" + Flag, "1207", "0");

            // ActiveX 控件和插件:对标记为可安全执行脚本的 ActiveX 控件执行脚本 ^  
            Comm.WriteLogs("IE-Regedit-Zones-1-2104-0" + "\r\n");
            AddOrEditKeyValue("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\" + Flag, "1405", "0");

            //允许网站打开没有地址或者状态栏的窗口在注册表设置启用
            Comm.WriteLogs("IE-Regedit-Zones-1-2104-0" + "\r\n");
            AddOrEditKeyValue("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\" + Flag, "2104", "0");

            //ActiveX 控件和插件:对标记为可安全执行脚本的 ActiveX 控件执行脚本 ^  
            Comm.WriteLogs("IE-Regedit-Zones-1-2104-0" + "\r\n");
            AddOrEditKeyValue("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\" + Flag, "2201", "0");


        }
        static void AddOrEditKeyValue(string path, string key, string value)
        {
            RegistryKey rk = Registry.CurrentUser.OpenSubKey(path, true);

            rk.SetValue(key, value, RegistryValueKind.DWord);

        }

        /// <summary>
        /// 添加可信站点和active为启用
        /// </summary>
        /// <param name="TrustedSite">可信站点</param>
        public static void SetTrustedSite(string TrustedSite)
        {
            try
            {
                //判断是否存在
                bool SiteHas = false;
                List<string> numbers = new List<string>();//用来存放Range编号
                RegistryKey key = Registry.CurrentUser.OpenSubKey("SoftWare", true);
                RegistryKey micr = key.OpenSubKey("Microsoft", true);
                RegistryKey win = micr.OpenSubKey("windows", true);
                RegistryKey cur = win.OpenSubKey("CurrentVersion", true);
                RegistryKey inter = cur.OpenSubKey("Internet Settings", true);
                RegistryKey zone = inter.OpenSubKey("ZoneMap", true);
                RegistryKey rang = zone.OpenSubKey("Ranges", true);

                //循环所有的range节点,如果要添加的IP已经存在则不操作,否则添加
                foreach (string u in rang.GetSubKeyNames())
                {
                    numbers.Add(u.Substring("Range".Length));//截取Range1后面的数字1
                    RegistryKey range = rang.OpenSubKey(u, true);//以可写的权限打开子节点

                    //如果该IP地址已经存在
                    if (range.GetValue(":Range").Equals(TrustedSite))
                    {
                        SiteHas = true;
                        if (range.GetValue("http") != null) break;//如果协议也正确说明IP已经是信任节点
                        range.SetValue("http", 2, RegistryValueKind.DWord);//添加协议对应的值
                    }

                }
                if (!SiteHas)
                {
                    //TrustedSite值:192.168.1.23
                    RegistryKey r100 = rang.CreateSubKey("Range" + findMinNumber(numbers));
                    r100.SetValue("http", 2);
                    r100.SetValue(":Range", TrustedSite);

                }
                key.Flush();
                key.Close();

            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 设置当前浏览器的版本
        /// </summary>
        public static void SetIEBrowserVersion()
        {
            int ieVersion = GetBrowserVersion();
            if (IfWindowsSupport())
            {
                SetWebBrowserFeatures(ieVersion < 11 ? ieVersion : 11);
            }
            else
            {
                //如果不支持IE8  则修改为当前系统的IE版本
                SetWebBrowserFeatures(ieVersion < 7 ? 7 : ieVersion);
            }

        }

        /// <summary>
        /// 查询系统环境是否支持IE8以上版本
        /// </summary>
        public static bool IfWindowsSupport()
        {
            bool isWin7 = Environment.OSVersion.Version.Major > 6;
            bool isSever2008R2 = Environment.OSVersion.Version.Major == 6
                && Environment.OSVersion.Version.Minor >= 1;

            if (!isWin7 && !isSever2008R2)
            {
                return false;
            }
            else return true;
        }
  
        /// 修改注册表信息来兼容当前程序  
        ///   
        /// </summary>  
        static void SetWebBrowserFeatures(int ieVersion)
        {
            // don't change the registry if running in-proc inside Visual Studio  
            if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
                return;
            //获取程序及名称  
            var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
            //得到浏览器的模式的值  
            UInt32 ieMode = GeoEmulationModee(ieVersion);
            var featureControlRegKey = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\";
            //设置浏览器对应用程序(appName)以什么模式(ieMode)运行  
            Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION",
                appName, ieMode, RegistryValueKind.DWord);
            // enable the features which are "On" for the full Internet Explorer browser  
            //不晓得设置有什么用  
            Registry.SetValue(featureControlRegKey + "FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION",
                appName, 1, RegistryValueKind.DWord);

        }
        /// <summary>  
        /// 获取浏览器的版本  
        /// </summary>  
        /// <returns></returns>  
        static int GetBrowserVersion()
        {
            int browserVersion = 0;
            using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
                RegistryKeyPermissionCheck.ReadSubTree,
                System.Security.AccessControl.RegistryRights.QueryValues))
            {
                var version = ieKey.GetValue("svcVersion");
                if (null == version)
                {
                    version = ieKey.GetValue("Version");
                    if (null == version)
                        throw new ApplicationException("Microsoft Internet Explorer is required!");
                }
                int.TryParse(version.ToString().Split('.')[0], out browserVersion);
            }
            //如果小于7  
            if (browserVersion < 7)
            {
                throw new ApplicationException("不支持的浏览器版本!");
            }
            return browserVersion;
        }
        /// <summary>  
        /// 通过版本得到浏览器模式的值  
        /// </summary>  
        /// <param name="browserVersion"></param>  
        /// <returns></returns>  
        static UInt32 GeoEmulationModee(int browserVersion)
        {
            UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode.   
            switch (browserVersion)
            {
                case 7:
                    mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.   
                    break;
                case 8:
                    mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.   
                    break;
                case 9:
                    mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.                      
                    break;
                case 10:
                    mode = 10000; // Internet Explorer 10.  
                    break;
                case 11:
                    mode = 11000; // Internet Explorer 11  
                    break;
            }
            return mode;
        }


    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值