コード例 #1
0
        // expected syntax should be
        // pathToExe.exe /install mysample-AddIn-packed.xll mysample-AddIn64-packed.xll '12.0,14.0,15.0'
        // pathToExe.exe /uninstall mysample-AddIn-packed.xll mysample-AddIn64-packed.xll '12.0,14.0,15.0'
        public static Parameters ExtractFromArgs(string[] args)
        {
            if (args.Length != 4)
            {
                throw new ArgumentException("Wrong number of arguments, should be 4 we found: " + args.Length);
            }
            var parameters = new Parameters();
            if (args[0] == "/install")
            {
                parameters.Command = Command.Install;
            }
            else if (args[0] == "/uninstall")
            {
                parameters.Command = Command.Uninstall;
            }
            else
            {
                throw new ArgumentException(@"There are two arguments possible: /install or /uninstall).");
            }

            parameters.XllName = args[1];
            parameters.Xll64Name = args[2];

            //The install directory of the xll is supposed to be the same as the .exe one.
            string directory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            parameters.InstallDirectory = directory;

            parameters.SupportedOfficeVersion = args[3].Split(',').ToList();

            return parameters;
        }
コード例 #2
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 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");
        }
コード例 #3
0
        public void RemoveHkcuOpenKey(Parameters parameters)
        {
            Console.WriteLine("Begin RemoveHKCUOpenKey");

            if (parameters.SupportedOfficeVersion.Count == 0)
            {
                throw new ApplicationException("There should be at least one office version supported");
            }

            foreach (string szOfficeVersionKey in parameters.SupportedOfficeVersion)
            {
                // only remove keys where office version is found
                string officeKey = SzBaseAddInKey + szOfficeVersionKey;
                Console.WriteLine("Try opening {0} HKCU key", officeKey);
                if (Registry.CurrentUser.OpenSubKey(officeKey, false) != null)
                {

                    string szKeyName = SzBaseAddInKey + szOfficeVersionKey + @"\Excel\Options";
                    Console.WriteLine("Try opening {0} HKCU key", szKeyName);
                    using (RegistryKey rkAddInKey = Registry.CurrentUser.OpenSubKey(szKeyName, true))
                    {
                        if (rkAddInKey != null)
                        {
                            Console.WriteLine("...key found!");
                            string[] szValueNames = rkAddInKey.GetValueNames();

                            foreach (string szValueName in szValueNames)
                            {
                                //unregister both 32 and 64 xll
                                if (szValueName.StartsWith("OPEN") && (rkAddInKey.GetValue(szValueName).ToString().Contains(parameters.Xll64Name) || rkAddInKey.GetValue(szValueName).ToString().Contains(parameters.XllName)))
                                {
                                    Console.WriteLine("Deletes the value {0}", szValueName);
                                    rkAddInKey.DeleteValue(szValueName);
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("... key not found.");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("... key does not exists.");
                }
            }

            Console.WriteLine("End RemoveHKCUOpenKey");
        }