Пример #1
0
        /// <summary>
        /// Check if settings have components that require admin rights, give option to restart as admin
        /// </summary>
        private static void RunAsAdminCheck()
        {
            //If not already running as admin
            if (!userAccessControl.IsRunAsAdmin())
            {
                #region Lync Custom Menus
                //If Lync custom menus are defined check if they need updating
                bool regRequiresUpdate = false;

                if (Settings.appSettings.Descendants("CustomMenuItems").Elements("MenuItem").Any())
                {
                    ModifyRegistry readKey = new ModifyRegistry();
                    readKey.RegistryView = RegistryView.Registry32;
                    readKey.RegistryHive = RegistryHive.LocalMachine;
                    readKey.ShowError    = true;

                    //Read MenuItems from XML
                    foreach (var CustomMenuItem in Settings.appSettings.Descendants("CustomMenuItems").Descendants("MenuItem"))
                    {
                        readKey.SubKey = "SOFTWARE\\Microsoft\\Office\\15.0\\Lync\\SessionManager\\Apps\\" +
                                         CustomMenuItem.Element("GUID").Value;

                        foreach (var CustomMenuItemValue in CustomMenuItem.Elements())
                        {
                            if (CustomMenuItemValue.Name.ToString() != "GUID")
                            {
                                string regKeyName       = CustomMenuItemValue.Name.ToString();
                                string regSettingsValue = CustomMenuItemValue.Value;
                                string regValue         = readKey.Read(regKeyName);

                                //MessageBox.Show("Sval: " + regSettingsValue + Environment.NewLine + "regVal: " + regValue);

                                if (regSettingsValue != regValue)
                                {
                                    regRequiresUpdate = true;
                                }
                            }
                        }
                    }
                }

                //If Lync custom menus need updating prompt to restart as admin
                if (regRequiresUpdate)
                {
                    if (MessageBox.Show(
                            "Application settings have enabled Lync custom menus, however you do not have the required permissions. " +
                            "To enable these settings run the application as admin, or remove this configuration from the applications settings." +
                            Environment.NewLine + Environment.NewLine + "Would you like to restart as admin?",
                            "Error creating Lync custom menus", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        userAccessControl.Elevate();
                    }
                }
                #endregion Lync Custom Menus
            }
        }
Пример #2
0
        public static void CreateLyncMenu()
        {
            //Get UAC status
            UserAccessControl uacLyncMenu = new UserAccessControl();

            try
            {
                if (Settings.appSettings.Descendants("CustomMenuItems").Elements("MenuItem").Any())
                {
                    ////Request to elevate if required
                    //if (!uacLyncMenu.IsRunAsAdmin())
                    //{
                    //    if (MessageBox.Show("Application settings have enabled Lync custom menus, however you do not have the required permissions. To avoid seeing this message again run the application as admin, or remove this configuration from application settings." + Environment.NewLine + Environment.NewLine + "Would you like to restart as admin?", "Error creating Lync custom menus", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    //    {
                    //        uacLyncMenu.Elevate();
                    //    }
                    //    else
                    //    {
                    //        return;
                    //    }
                    //}

                    if (uacLyncMenu.IsRunAsAdmin())
                    {
                        //Read MenuItems from XML
                        foreach (var CustomMenuItem in Settings.appSettings.Descendants("CustomMenuItems").Descendants("MenuItem"))
                        {
                            //string GUID = CustomMenuItem.Element("GUID").Value;
                            //string extensibleMenu = CustomMenuItem.Element("ExtensibleMenu").Value;
                            //string name = CustomMenuItem.Element("Name").Value;
                            //string path = CustomMenuItem.Element("Path").Value;

                            //MessageBox.Show(GUID + extensibleMenu + name + path);

                            //Write registry entry
                            //string regSubKey = "Software\\Test\\" + CustomMenuItem.Element("GUID").Value;
                            string regSubKey = "SOFTWARE\\Microsoft\\Office\\15.0\\Lync\\SessionManager\\Apps\\" + CustomMenuItem.Element("GUID").Value;

                            foreach (var CustomMenuItemValue in CustomMenuItem.Elements())
                            {
                                if (CustomMenuItemValue.Name.ToString() != "GUID")
                                {
                                    //If Reg kind REG_SZ
                                    if (CustomMenuItemValue.Name == "ExtensibleMenu" ||
                                        CustomMenuItemValue.Name == "Path" ||
                                        CustomMenuItemValue.Name == "Name" ||
                                        CustomMenuItemValue.Name == "ApplicationInstallPath")
                                    {
                                        if (CustomMenuItemValue.Value != null || CustomMenuItemValue.Value != "")
                                        {
                                            //MessageBox.Show(CustomMenuItemValue.Name.ToString() + ":" + CustomMenuItemValue.Value.ToString());
                                            ModifyRegistry addKey = new ModifyRegistry();

                                            addKey.RegistryView = RegistryView.Registry32; //specifies to use the x64 registry node for x32 application. If you request a 64-bit view on a 32-bit operating system, the returned keys will be in the 32-bit view.
                                            addKey.RegistryHive = RegistryHive.LocalMachine;
                                            addKey.ShowError    = true;

                                            string regKeyName = CustomMenuItemValue.Name.ToString();
                                            string regValue   = CustomMenuItemValue.Value.ToString();
                                            addKey.SubKey = regSubKey;

                                            addKey.Write(regKeyName, regValue, RegistryValueKind.String);
                                        }
                                    }
                                    //If Reg kind DWORD
                                    else if (CustomMenuItemValue.Name == "ApplicationType" ||
                                             CustomMenuItemValue.Name == "SessionType")
                                    {
                                        if (CustomMenuItemValue.Value != null || CustomMenuItemValue.Value != "")
                                        {
                                            ModifyRegistry addKey = new ModifyRegistry();

                                            addKey.RegistryView = RegistryView.Registry32; //specifies to use the x64 registry node for x32 application. If you request a 64-bit view on a 32-bit operating system, the returned keys will be in the 32-bit view.
                                            addKey.RegistryHive = RegistryHive.LocalMachine;
                                            addKey.ShowError    = true;

                                            string regKeyName = CustomMenuItemValue.Name.ToString();
                                            string regValue   = CustomMenuItemValue.Value.ToString();
                                            addKey.SubKey = regSubKey;

                                            addKey.Write(regKeyName, regValue, RegistryValueKind.DWord);
                                        }
                                    }
                                }

                                //Add Application Type
                                //ModifyRegistry addKeyAppType = new ModifyRegistry();
                                //addKeyAppType.RegistryView = RegistryView.Registry32;
                                //addKeyAppType.RegistryHive = RegistryHive.LocalMachine;
                                //addKeyAppType.SubKey = regSubKey;
                                //addKeyAppType.ShowError = true;
                                //addKeyAppType.Write("ApplicationType", 1, RegistryValueKind.DWord);
                            }

                            #region No longer required
                            //Read Lync custom menu items from XML
                            //Delete the existing menu items
                            //Read existing subkeys
                            //ModifyRegistry readChildSubKeys = new ModifyRegistry();
                            //readChildSubKeys.RegistryView = RegistryView.Registry64;//specifies to use the x64 registry node for x32 application. If you request a 64-bit view on a 32-bit operating system, the returned keys will be in the 32-bit view.
                            //readChildSubKeys.RegistryHive = RegistryHive.LocalMachine;
                            //readChildSubKeys.SubKey = "Software\\Test";
                            //readChildSubKeys.ShowError = true;
                            //string[] xxxx = readChildSubKeys.ReadChildSubKeys();

                            //foreach (var item in xxxx)
                            //{
                            //Delete found subkeys
                            //MessageBox.Show(item);
                            //}

                            //ModifyRegistry deleteKey = new ModifyRegistry();
                            //deleteKey.RegistryView = RegistryView.Registry64;//specifies to use the x64 registry node for x32 application. If you request a 64-bit view on a 32-bit operating system, the returned keys will be in the 32-bit view.
                            //deleteKey.RegistryHive = RegistryHive.LocalMachine;
                            //deleteKey.SubKey = "Software\\Test";
                            //deleteKey.ShowError = true;
                            //deleteKey.DeleteSubKeyTree();

                            //Generate GUID
                            //Guid id = Guid.NewGuid();
                            #endregion OLD
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //AAAAAAAAAAARGH, an error!
                Shared.MB("Error adding Lync custom menus : " + ex.Message, "ERROR!");
            }
        }