public OfficePathsReturn GetOfficePathList()
    {
        var officeKeys = new List<string>()
        {
            @"SOFTWARE\Microsoft\Office",
            @"SOFTWARE\Wow6432Node\Microsoft\Office"
        };

        var clickToRunList = new List<OfficeInstall>();

        var pathReturn = new OfficePathsReturn()
        {
            ClickToRunList = new List<OfficeInstall>(),
            VersionList = new List<string>(),
            PathList = new List<string>(),
            PackageList = new List<string>(),
            ClickToRunPathList = new List<string>(),
            ConfigItemList = new List<string>()
        };


        foreach (var regKey in officeKeys)
        {
            var officeVersion = GetRegistrySubKeys(regKey);
            var c2RRegPath = regKey + @"\ClickToRun\Configuration";
            var c2R16Key = GetRegistryKey(c2RRegPath);
            if (c2R16Key != null)
            {
                clickToRunList.Add(new OfficeInstall
                {
                    InstallPath = GetRegistryValue(c2RRegPath, "InstallationPath"),
                    Bitness = GetRegistryValue(c2RRegPath, "Platform"),
                    ClientCulture = GetRegistryValue(c2RRegPath, "ClientCulture"),
                    ClickToRun = true
                });
            }

            foreach (var key in officeVersion)
            {
                var match = Regex.Match(key, @"\d{2}\.\d");
                if (match.Success)
                {
                    if (!pathReturn.VersionList.Contains(key))
                    {
                        pathReturn.VersionList.Add(key);
                    }

                    var path = regKey + @"\" + key;
                    var configPath = path + @"\Common\Config";

                    var configItems = GetRegistrySubKeys(configPath);
                    if (configItems != null)
                    {
                        pathReturn.ConfigItemList.AddRange(from configId in configItems where !string.IsNullOrEmpty(configId) select configId.ToUpper());
                    }

                    var cltr = new OfficeInstall();

                    var packagePath = path + @"\Common\InstalledPackages";
                    var clickToRunPath = path + @"\ClickToRun\Configuration";
                    var officeLangResourcePath = path + @"\Common\LanguageResources";
                    var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

                    var virtualInstallPath = GetRegistryValue(clickToRunPath, "InstallationPath");
                    var mainLangId = GetRegistryValue(officeLangResourcePath, "SKULanguage");
                    if (string.IsNullOrEmpty(mainLangId))
                    {
                        var mainlangCulture = cultures.FirstOrDefault(c => c.LCID.ToString() == mainLangId);

                        if (mainlangCulture != null)
                        {
                            cltr.ClientCulture = mainlangCulture.Name;
                        }
                    }

                    var officeLangPath = path + @"\Common\LanguageResources\InstalledUIs";
                    var langValues = GetRegistrySubKeys(officeLangPath);

                    CultureInfo langCulture = null;
                    if (langValues != null)
                    {
                        foreach (var langValue in langValues)
                        {
                            langCulture = cultures.FirstOrDefault(c => c.LCID.ToString() == langValue);
                        }
                    }

                    if (string.IsNullOrEmpty(virtualInstallPath))
                    {
                        clickToRunPath = regKey + @"\ClickToRun\Configuration";
                        virtualInstallPath = GetRegistryValue(clickToRunPath, "InstallationPath");
                    }

                    if (!string.IsNullOrEmpty(virtualInstallPath))
                    {
                        if (virtualInstallPath == null) virtualInstallPath = "";
                        if (!pathReturn.ClickToRunPathList.Contains(virtualInstallPath.ToUpper()))
                        {
                            pathReturn.ClickToRunPathList.Add(virtualInstallPath.ToUpper());
                        }

                        cltr.InstallPath = virtualInstallPath;
                        cltr.Bitness = GetRegistryValue(clickToRunPath, "Platform");
                        cltr.ClientCulture = GetRegistryValue(clickToRunPath, "ClientCulture");
                        cltr.ClickToRun = true;
                        clickToRunList.Add(cltr);
                    }

                    var packageItems = GetRegistrySubKeys(packagePath);
                    var officeItems = GetRegistrySubKeys(path);

                    foreach (var itemKey in officeItems)
                    {
                        var itemPath = path + @"\" + itemKey;
                        var installRootPath = itemPath + @"\InstallRoot";

                        //HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\15.0\Common\InstallRoot

                        var filePath = GetRegistryValue(installRootPath, "Path");

                        if (string.IsNullOrEmpty(filePath)) continue;

                        if (!pathReturn.PathList.Contains(filePath))
                        {
                            pathReturn.PathList.Add(filePath);
                        }
                    }

                    if (packageItems != null)
                    {
                        foreach (var packageGuid in packageItems)
                        {
                            var packageItemPath = packagePath + @"\" + packageGuid;
                            var packageName = GetRegistryValue(packageItemPath, null);
                            if (!pathReturn.PackageList.Contains(packageName))
                            {
                                if (!string.IsNullOrEmpty(packageName))
                                {
                                    pathReturn.PackageList.Add(packageName.Replace(" ", "").ToLower());
                                }
                            }
                        }
                    }

                }
            }
        }

        return pathReturn;
    }
Ejemplo n.º 2
0
    public OfficePathsReturn GetOfficePathList()
    {
        var officeKeys = new List <string>()
        {
            @"SOFTWARE\Microsoft\Office",
            @"SOFTWARE\Wow6432Node\Microsoft\Office"
        };

        var clickToRunList = new List <OfficeInstall>();

        var pathReturn = new OfficePathsReturn()
        {
            ClickToRunList     = new List <OfficeInstall>(),
            VersionList        = new List <string>(),
            PathList           = new List <string>(),
            PackageList        = new List <string>(),
            ClickToRunPathList = new List <string>(),
            ConfigItemList     = new List <string>()
        };


        foreach (var regKey in officeKeys)
        {
            var officeVersion = GetRegistrySubKeys(regKey);
            var c2RRegPath    = regKey + @"\ClickToRun\Configuration";
            var c2R16Key      = GetRegistryKey(c2RRegPath);
            if (c2R16Key != null)
            {
                clickToRunList.Add(new OfficeInstall
                {
                    InstallPath   = GetRegistryValue(c2RRegPath, "InstallationPath"),
                    Bitness       = GetRegistryValue(c2RRegPath, "Platform"),
                    ClientCulture = GetRegistryValue(c2RRegPath, "ClientCulture"),
                    ClickToRun    = true
                });
            }

            foreach (var key in officeVersion)
            {
                var match = Regex.Match(key, @"\d{2}\.\d");
                if (match.Success)
                {
                    if (!pathReturn.VersionList.Contains(key))
                    {
                        pathReturn.VersionList.Add(key);
                    }

                    var path       = regKey + @"\" + key;
                    var configPath = path + @"\Common\Config";

                    var configKey   = Registry.LocalMachine.OpenSubKey(configPath);
                    var configItems = configKey?.GetSubKeyNames();

                    if (configItems != null)
                    {
                        pathReturn.ConfigItemList.AddRange(from configId in configItems where !string.IsNullOrEmpty(configId) select configId.ToUpper());
                    }

                    var cltr = new OfficeInstall();

                    var packagePath            = path + @"\Common\InstalledPackages";
                    var clickToRunPath         = path + @"\ClickToRun\Configuration";
                    var officeLangResourcePath = path + @"\Common\LanguageResources";
                    var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

                    var virtualInstallPath = GetRegistryValue(clickToRunPath, "InstallationPath")?.ToString();
                    var mainLangId         = GetRegistryValue(officeLangResourcePath, "SKULanguage")?.ToString();
                    if (string.IsNullOrEmpty(mainLangId))
                    {
                        var mainlangCulture = cultures.FirstOrDefault(c => c.LCID.ToString() == mainLangId);

                        if (mainlangCulture != null)
                        {
                            cltr.ClientCulture = mainlangCulture.Name;
                        }
                    }

                    var officeLangPath = path + @"\Common\LanguageResources\InstalledUIs";
                    var offLangPathKey = Registry.LocalMachine.OpenSubKey(officeLangPath);
                    var langValues     = offLangPathKey?.GetSubKeyNames();

                    CultureInfo langCulture = null;
                    if (langValues != null)
                    {
                        foreach (var langValue in langValues)
                        {
                            langCulture = cultures.FirstOrDefault(c => c.LCID.ToString() == langValue);
                        }
                    }

                    if (string.IsNullOrEmpty(virtualInstallPath))
                    {
                        clickToRunPath     = regKey + @"\ClickToRun\Configuration";
                        virtualInstallPath = GetRegistryValue(clickToRunPath, "InstallationPath");
                    }

                    if (!string.IsNullOrEmpty(virtualInstallPath))
                    {
                        if (!pathReturn.ClickToRunPathList.Contains(virtualInstallPath?.ToUpper()))
                        {
                            pathReturn.ClickToRunPathList.Add(virtualInstallPath?.ToUpper());
                        }

                        cltr.InstallPath   = virtualInstallPath;
                        cltr.Bitness       = GetRegistryValue(clickToRunPath, "Platform");
                        cltr.ClientCulture = GetRegistryValue(clickToRunPath, "ClientCulture");
                        cltr.ClickToRun    = true;
                        clickToRunList.Add(cltr);
                    }

                    var packageItems = Registry.LocalMachine.OpenSubKey(packagePath)?.GetSubKeyNames();
                    var officeItems  = Registry.LocalMachine.OpenSubKey(path)?.GetSubKeyNames();

                    foreach (var itemKey in officeItems)
                    {
                        var itemPath        = path + @"\" + itemKey;
                        var installRootPath = itemPath + @"\InstallRoot";

                        //HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\15.0\Common\InstallRoot

                        var filePath = GetRegistryValue(installRootPath, "Path");

                        if (string.IsNullOrEmpty(filePath))
                        {
                            continue;
                        }

                        if (!pathReturn.PathList.Contains(filePath))
                        {
                            pathReturn.PathList.Add(filePath);
                        }
                    }

                    if (packageItems != null)
                    {
                        foreach (var packageGuid in packageItems)
                        {
                            var packageItemPath = packagePath + @"\" + packageGuid;
                            var packageName     = Registry.LocalMachine.OpenSubKey(packageItemPath)?.GetValue(null)?.ToString();
                            if (!pathReturn.PackageList.Contains(packageName))
                            {
                                if (!string.IsNullOrEmpty(packageName))
                                {
                                    pathReturn.PackageList.Add(packageName.Replace(" ", "").ToLower());
                                }
                            }
                        }
                    }
                }
            }
        }

        return(pathReturn);
    }