zoukankan      html  css  js  c++  java
  • 获取软件在注册表的安装信息

             /// <summary>
            /// 获取软件在注册表的安装信息
            /// 软件都会在这个注册表下填写自己的安装信息
            /// HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Paths
            /// </summary>
            /// <param name="softName">软件名称</param>
            /// <param name="path">返回软件安装路径</param>
            /// <returns>是否找到注册表的安装路径</returns>
            public static bool TryGetSoftwarePath(string softName, out string path)
            {
                string strPathResult = string.Empty;
                string strKeyName = "";     //"(Default)" key, which contains the intalled path
                object objResult = null;
    
                Microsoft.Win32.RegistryValueKind regValueKind;
                Microsoft.Win32.RegistryKey regKey = null;
                Microsoft.Win32.RegistryKey regSubKey = null;
    
                try
                {
                    //Read the key
                    regKey = Microsoft.Win32.Registry.LocalMachine;
                    regSubKey = regKey.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionApp Paths" + softName.ToString() + ".exe", false);
    
                    //Read the path
                    objResult = regSubKey.GetValue(strKeyName);
                    regValueKind = regSubKey.GetValueKind(strKeyName);
    
                    //Set the path
                    if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
                    {
                        strPathResult = objResult.ToString();
                    }
                }
                catch (System.Security.SecurityException ex)
                {
                    throw new System.Security.SecurityException("You have no right to read the registry!", ex);
                }
                catch (Exception ex)
                {
                    throw new Exception("Reading registry error!", ex);
                }
                finally
                {
    
                    if (regKey != null)
                    {
                        regKey.Close();
                        regKey = null;
                    }
    
                    if (regSubKey != null)
                    {
                        regSubKey.Close();
                        regSubKey = null;
                    }
                }
    
                if (strPathResult != string.Empty)
                {
                    //Found
                    path = strPathResult;
                    return true;
                }
                else
                {
                    //Not found
                    path = null;
                    return false;
                }
            }


  • 相关阅读:
    rsync使用
    文件系统、mkdir、touch、nano、cp笔记
    man/ls/clock/date/echo笔记
    Python之路,Day2
    Python之路,Day1
    自动化部署nginx负载均衡及监控短信报警
    NO.11天作业
    Tiny C Compiler简介-wiki
    stm32中使用cubemx配置freertos的信号量大小
    c99的新功能
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3258005.html
Copyright © 2011-2022 走看看