程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 獲取軟件在注冊表的安裝信息

獲取軟件在注冊表的安裝信息

編輯:C#入門知識

 /// <summary>  
/// 獲取軟件在注冊表的安裝信息  
/// 軟件都會在這個注冊表下填寫自己的安裝信息  
/// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App 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(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App 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; 
    } 
} 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved