C# 获取常见软件的安装路径(调用注册表)

很多软件安装位置不一样,但是他们基本上会在注册表的同一个位置写下自己的名字和程序路径,这个位置就是:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

所以只要去这个地方找软件路径就可以了。

你可以添加你知道的正确的文件名,就是它在注册表中的名字。比如office word在注册表中叫winword

最终拿到的是那个exe的完全路径,如果要文件夹的话就用Path.GetDirectoryName()就行了

public enum Softwares 
{ 
    //The names are the same with the registry names. 
    //You can add any software exists in the "regedit" path: 
    //HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths 

    EXCEL,      //Office Excel 
    WINWORD,    //Office Word 
    MSACCESS,   //Office Access 
    POWERPNT,   //Office PowerPoint 
    OUTLOOK,    //Office Outlook 
    INFOPATH,   //Office InfoPath 
    MSPUB,      //Office Publisher 
    VISIO,      //Office Visio 
    IEXPLORE,   //IE 
    ITUNES      //Apple ITunes 
    //......... 
} 

public class SoftwareOperator 
{ 
    //When you do not want to use string name, then use the Enum instead 
    public static bool TryGetSoftwarePath(Softwares softName, out string path) 
    { 
        return TryGetSoftwarePath(softName.ToString(), out path); 
    } 

    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; 
        } 
    } 
} 


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值