示例#1
0
        public void CreateOpenHkcuKey(Parameters parameters)
        {
            if (parameters.SupportedOfficeVersion.Count == 0)
            {
                throw new ApplicationException("There should be at least one office version supported");
            }

            var  registryAdapator = new RegistryAbstractor();
            bool foundOffice      = false;

            foreach (string szOfficeVersionKey in parameters.SupportedOfficeVersion)
            {
                double nVersion = double.Parse(szOfficeVersionKey, NumberStyles.Any, CultureInfo.InvariantCulture);

                Console.WriteLine("Retrieving Registry Information for : " + SzBaseAddInKey + szOfficeVersionKey);

                // get the OPEN keys from the Software\Microsoft\Office\[Version]\Excel\Options key, skip if office version not found.
                string excelBaseKey = SzBaseAddInKey + szOfficeVersionKey + @"\Excel";
                // Software\Microsoft\Office\[Version]\Excel
                if (IsOfficeExcelInstalled(excelBaseKey)) // this version is install on the Machine, so we have to consider it for the HKCU
                {
                    if (!foundOffice)
                    {
                        foundOffice = true;
                    }

                    string excelOptionKey = excelBaseKey + @"\Options";

                    // It is very important to Open or Create see https://github.com/bpatra/ExcelDNAWixInstallerLM/issues/9
                    using (RegistryKey rkExcelXll = registryAdapator.OpenOrCreateHkcuKey(excelOptionKey))
                    {
                        string szXllToRegister = GetAddInName(parameters.XllName, parameters.Xll64Name, szOfficeVersionKey, nVersion);
                        // for a localmachine install the xll's should be in the installFolder
                        string fullPathToXll = Path.Combine(parameters.InstallDirectory, szXllToRegister);

                        Console.WriteLine("Success finding HKCU key for : " + excelOptionKey);
                        string[] szValueNames = rkExcelXll.GetValueNames();
                        bool     bIsOpen      = false;
                        int      nMaxOpen     = -1;

                        // check every value for OPEN keys
                        foreach (string szValueName in szValueNames)
                        {
                            Console.WriteLine(string.Format("Examining value {0}", szValueName));
                            // if there are already OPEN keys, determine if our key is installed
                            if (szValueName.StartsWith("OPEN"))
                            {
                                int nOpenVersion = int.TryParse(szValueName.Substring(4), out nOpenVersion) ? nOpenVersion : 0;
                                int nNewOpen     = szValueName == "OPEN" ? 0 : nOpenVersion;
                                if (nNewOpen > nMaxOpen)
                                {
                                    nMaxOpen = nNewOpen;
                                }

                                // if the key is our key, set the open flag
                                // NOTE: this line means if the user has changed its office from 32 to 64 (or conversly) without removing the addin then we will not update the key properly
                                // The user will have to uninstall addin before installing it again
                                if (rkExcelXll.GetValue(szValueName).ToString().Contains(szXllToRegister))
                                {
                                    bIsOpen = true;
                                    Console.WriteLine("Already found the OPEN key " + excelOptionKey);
                                }
                            }
                        }


                        // if adding a new key
                        if (!bIsOpen)
                        {
                            string value = "/R \"" + fullPathToXll + "\"";
                            string keyToUse;
                            if (nMaxOpen == -1)
                            {
                                keyToUse = "OPEN";
                            }
                            else
                            {
                                keyToUse = "OPEN" + (nMaxOpen + 1).ToString(CultureInfo.InvariantCulture);
                            }
                            rkExcelXll.SetValue(keyToUse, value);
                            Console.WriteLine("Set {0} key with {1} value", keyToUse, value);
                        }
                    }
                }
                else
                {
                    Console.WriteLine(
                        "Unable to retrieve Office information in HKLM key: {0}. This version of Office might not be installed.",
                        excelBaseKey);
                }
            }

            if (!foundOffice)
            {
                throw new ApplicationException("No Excel found in HKLM.");
            }

            Console.WriteLine("End CreateOpenHKCUKey");
        }
        public void CreateOpenHkcuKey(Parameters parameters)
        {
            if (parameters.SupportedOfficeVersion.Count == 0)
            {
                throw new ApplicationException("There should be at least one office version supported");
            }
            var registryAdapator = new RegistryAbstractor();

            bool foundOffice = false;

            foreach (string szOfficeVersionKey in parameters.SupportedOfficeVersion)
            {
                double nVersion = double.Parse(szOfficeVersionKey, NumberStyles.Any, CultureInfo.InvariantCulture);

                Console.WriteLine("Retrieving Registry Information for : " + SzBaseAddInKey + szOfficeVersionKey);

                // get the OPEN keys from the Software\Microsoft\Office\[Version]\Excel\Options key, skip if office version not found.
                string excelBaseKey = SzBaseAddInKey + szOfficeVersionKey + @"\Excel";
                //Software\Microsoft\Office\[Version]\Excel
                if (IsOfficeExcelInstalled(excelBaseKey))//this version is install on the Machine, so we have to consider it for the HKCU
                {
                    if (!foundOffice) foundOffice = true;

                    string excelOptionKey = excelBaseKey +  @"\Options";

                    //It is very important to Open or Create see https://github.com/bpatra/ExcelDNAWixInstallerLM/issues/9
                    using (RegistryKey rkExcelXll = registryAdapator.OpenOrCreateHkcuKey(excelOptionKey))
                    {
                        string szXllToRegister = GetAddInName(parameters.XllName, parameters.Xll64Name, szOfficeVersionKey,
                                                        nVersion);
                        //for a localmachine install the xll's should be in the installFolder
                        string fullPathToXll = Path.Combine(parameters.InstallDirectory, szXllToRegister);

                        Console.WriteLine("Success finding HKCU key for : " + excelOptionKey);
                        string[] szValueNames = rkExcelXll.GetValueNames();
                        bool bIsOpen = false;
                        int nMaxOpen = -1;

                        // check every value for OPEN keys
                        foreach (string szValueName in szValueNames)
                        {
                            Console.WriteLine(string.Format("Examining value {0}", szValueName));
                            // if there are already OPEN keys, determine if our key is installed
                            if (szValueName.StartsWith("OPEN"))
                            {
                                int nOpenVersion = int.TryParse(szValueName.Substring(4), out nOpenVersion) ? nOpenVersion : 0;
                                int nNewOpen = szValueName == "OPEN" ? 0 : nOpenVersion;
                                if (nNewOpen > nMaxOpen)
                                {
                                    nMaxOpen = nNewOpen;
                                }

                                // if the key is our key, set the open flag
                                //NOTE: this line means if the user has changed its office from 32 to 64 (or conversly) without removing the addin then we will not update the key properly
                                //The user will have to uninstall addin before installing it again
                                if (rkExcelXll.GetValue(szValueName).ToString().Contains(szXllToRegister))
                                {
                                    bIsOpen = true;
                                    Console.WriteLine("Already found the OPEN key " + excelOptionKey);
                                }
                            }
                        }

                        // if adding a new key
                        if (!bIsOpen)
                        {
                            string value = "/R \"" + fullPathToXll + "\"";
                            string keyToUse;
                            if (nMaxOpen == -1)
                            {
                                keyToUse = "OPEN";
                            }
                            else
                            {
                                keyToUse = "OPEN" + (nMaxOpen + 1).ToString(CultureInfo.InvariantCulture);

                            }
                            rkExcelXll.SetValue(keyToUse, value);
                            Console.WriteLine("Set {0} key with {1} value", keyToUse, value);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Unable to retrieve HKLM key for: " + excelBaseKey + ". This version of Office might not be installed.");
                }
            }

            if(!foundOffice) throw new ApplicationException("No Excel found in HKLM");

            Console.WriteLine("End CreateOpenHKCUKey");
        }