zoukankan      html  css  js  c++  java
  • C#winform检测电脑安装的.netframework版本和是否安装了某软件

    代码如下:

            //C#获取已安装 .NET Framework 版本
            private static string[] GetDotNetVersions()
            {
                DirectoryInfo[] directories = new DirectoryInfo(
                    Environment.SystemDirectory + @"..Microsoft.NETFramework").GetDirectories("v?.?.*");
                ArrayList list = new ArrayList();
                foreach (DirectoryInfo info2 in directories)
                {
                    list.Add(info2.Name.Substring(1));
                }
                return (list.ToArray(typeof(string)) as string[]);
            }
    
            /// <summary>  
            /// 确认电脑上是否安装有某个程序
            /// </summary>  
            /// <param name="softWareName">程序安装后的名称</param>
            /// <returns>true: 有安裝, false:沒有安裝</returns>  
            private static bool CheckSoftWartInstallState(string softWareName = "Adobe Reader")
            {
                Microsoft.Win32.RegistryKey uninstallNode =
                    Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionUninstall", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.ReadKey);
                foreach (string subKeyName in uninstallNode.GetSubKeyNames())
                {
                    Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName);
                    object displayName = subKey.GetValue("DisplayName");
                    if (displayName != null)
                    {
                        if (displayName.ToString().Contains(softWareName))
                        {
                            return true;
                            // MessageBox.Show(displayName.ToString());  
                        }
                    }
                }
                return false;
            }
    
            /// <summary>
            /// 程序运行前检测
            /// </summary>
            public static void ApplicationBeforeRunCheck()
            {
                string[] deviceName = { "PL-2303 USB-to-Serial",
                                          "Microsoft ReportViewer 2010 Redistributable",
                                          "二代证读写机具USB驱动"};
                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                string[] netList = GetDotNetVersions();
                bool dotNetIsOk = false;
                for (int i = 0; i < netList.Length; i++)
                {
                    if (netList[i][0] == 4)
                        dotNetIsOk = true;
                }
                if (!dotNetIsOk)
                {
                    Process.Start(Path.Combine(baseDirectory, "4.exe"));
                    while (true)
                    {
                        for (int i = 0; i < netList.Length; i++)
                            if (netList[i][0] == 4) break;
                        Thread.Sleep(1000);
                    }
                }
                for (int i = 0; i < deviceName.Length; i++)
                {
                    if (!CheckSoftWartInstallState(deviceName[i]))
                    {
                        Process.Start(Path.Combine(baseDirectory, ((i + 1) + ".exe")));
                        while (true)
                        {
                            if (CheckSoftWartInstallState(deviceName[i]))
                                break;
                            Thread.Sleep(1000);
                        }
                    }
                }
                //软件安装完成            
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey rk2 = rk.CreateSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");
                rk2.SetValue("jkytj", baseDirectory);
                rk2.Close();
                rk.Close();
                //软件自启完成
            }



  • 相关阅读:
    All in One 你想知道的 hacker 技术都在这里
    5 个 Git 工作流,改善你的开发流程
    完全基于 Java 的开源深度学习平台,亚马逊的大佬带你上手
    感谢 Vue.js 拯救我这个前端渣渣,让 PowerJob 有了管理后台界面
    linux报错Loading mirror speeds from cached hostfile解决方法
    Linux使用mailx通过第三方SMTP发送邮件,带附件操作
    打包发送邮件
    springboot项目中使用设计模式一策略模式
    Spring Boot 开发 WebService 服务
    常见限流算法介绍(漏桶算法、令牌桶算法)及实现--待整理
  • 原文地址:https://www.cnblogs.com/myesn/p/5601600.html
Copyright © 2011-2022 走看看