c#: 判断Firefox是否安装

1、源起:

KV项目需要给浏览器安装下载插件,就需要判断是否安装对应浏览器,发现判断卸载目录方法,32位程序在.net 2.0运行环境下,常规方法不能访问64位注册表位置,导致不能判断。

 

2、卸载键值

@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"

@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"

 

所有安装过的程序,其若支持卸载,键值都在此处。

32位系统,以@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"做为key值访问,其也只能定位到@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"这里,不能直接访问64位值位。

因此,以下代码判断,对于64位系统,判断不到。也就是说,下面代码,直判断到Wow6432节点!

        private static string FirefoxInstallLoation()
        {
            const string unInsPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\";
            const string unInsPath64 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\";

            string firefoxLocation = string.Empty;
            try
            {
//这里,32位程序,实际定位到的,是Wow6432下节点
var unInsKey = Registry.LocalMachine.OpenSubKey(unInsPath); if (unInsKey == null) return string.Empty; string[] unInsKeyNames = unInsKey.GetSubKeyNames(); foreach (var name in unInsKeyNames) { if (name.Contains("Mozilla Firefox")) { firefoxLocation = unInsKey.OpenSubKey(name).GetValue("InstallLocation", string.Empty).ToString(); if (Directory.Exists(firefoxLocation)) return firefoxLocation; } }
//它仍是定位到Wow6432下 unInsKey
= Registry.LocalMachine.OpenSubKey(unInsPath64); if (unInsKey == null) return string.Empty; unInsKeyNames = unInsKey.GetSubKeyNames(); foreach (var name in unInsKeyNames) { if (name.Contains("Mozilla Firefox")) { firefoxLocation = unInsKey.OpenSubKey(name).GetValue("InstallLocation", string.Empty).ToString(); if (Directory.Exists(firefoxLocation)) return firefoxLocation; } } } catch { } return firefoxLocation; }

其始终定位到的,是此处:

 

3、灵光乍现,App Paths

于是寻找其它方法,还操注册表的心,真给搜出来个东西:

@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe"

此键值,在64、32位下都存在,其程序路径、安装路径都有了,看来找对了地方,写代码如下:

        //检测Chrome卸载程序判断其是否安装
        private static bool CheckFirefoxInstalled()
        {
            return File.Exists(FirefoxInstallLoation());
        }

        private static string FirefoxInstallLoation()
        {
            const string firefoxAppPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe";
            try
            {
                var firefoxKey = Registry.LocalMachine.OpenSubKey(firefoxAppPath);
                if (firefoxKey == null)
                    return string.Empty;
                return firefoxKey.GetValue(string.Empty).ToString();
            }
            catch
            {
            }
            return string.Empty;
        }

验证判断正常,问题解决!

 

 4、参数设置判断效果

转载于:https://www.cnblogs.com/crwy/p/7308689.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值