首页 > 技术文章 > 判断本机是否安装Microsoft Office或者wps

starpnd 2014-01-08 17:11 原文

网上通用的方法是Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Kingsoft\Office\6.0\common")中获取WPS安装信息,由于本机是win7 64位系统,实际安装后注册表写入路径为SOFTWARE\Wow6432Node\Kingsoft\Office\6.0\common,本机安装的是WPS2013,发现实际上common节点下并没有可供判断的值,并没有存储WPS安装路径,经测试,发现安装路径实际写入路径为HKEY_CURRENT_USER\Software\Kingsoft\Office\6.0\common,因此此处用这个路径进行判断,估计是WPS版本问题,此方法后期需要在实际应用中验证

判断本机是否安装wps方法:

private bool isWpsInstall()
        {
            bool isInstall = false;
            RegistryKey wpsLibrary = Registry.CurrentUser.OpenSubKey(@"Software\Kingsoft\Office\6.0\common");
            if (wpsLibrary != null)
            {
                if (wpsLibrary.GetValue("InstallRoot") != null)
                {
                    string strpath = wpsLibrary.GetValue("InstallRoot").ToString();
                    if (File.Exists(strpath + @"\office6\wps.exe"))
                    {
                        isInstall = true;
                    }
                }
            }
            return isInstall;
        }

由于本机是win7 64位系统,office 2007安装后,注册表写的位置是在SOFTWARE\Wow6432Node\Microsoft节点之下因此写了officeLibrary1进行判断,考虑到32位系统,注册表的位置直接在SOFTWARE\Microsoft节点之下,因此增加了officeLibrary0进行判断,满足任一种,则判断位office已安装,但是实际测试的时候,发现本机win7 64位下也可以用officeLibrary0取到值,但实际本机的SOFTWARE\Microsoft\Office节点下并没有对应的节点,因此,实际上用officeLibrary0进行判断即可,具体原因未知

判断本机是否安装office代码:

参数officever输入要验证的office版本,例如office2007为12.0

private bool isOfficeInstall(string officever)
        {
            bool isInstall = false;            
            RegistryKey officeLibrary0 = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Office\" + officever + @"\Common\InstallRoot");
            RegistryKey officeLibrary1 = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Office\" + officever + @"\Common\InstallRoot");
            if (officeLibrary0 != null)
            {
                if (officeLibrary0.GetValue("Path") != null)
                {
                    string strpath = officeLibrary0.GetValue("Path").ToString();
                    if (File.Exists(strpath + "WINWORD.EXE"))
                    {
                        isInstall = true;
                    }
                }
            }
            if (officeLibrary1 != null)
            {
                if (officeLibrary1.GetValue("Path") != null)
                {
                    string strpath = officeLibrary1.GetValue("Path").ToString();
                    if (File.Exists(strpath + "WINWORD.EXE"))
                    {
                        isInstall = true;
                    }
                }
            }
            return isInstall;
        }

 

推荐阅读