コード例 #1
0
ファイル: Program.cs プロジェクト: Atreidae/UCExtend
        /// <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
ファイル: Lync.cs プロジェクト: UCGeeks/UCExtend
        public static void RegistryWatcher()
        {
            //Monitor registry for changes, and on change update/revert
            try
            {
                //Read MenuItems from XML
                foreach (var registryWatcher in Settings.appSettings.Descendants("RegistryWatchers").Descendants("RegistryWatcher"))
                {
                    string registryHive = registryWatcher.Element("RegistryHive").Value;
                    string registryView = registryWatcher.Element("RegistryView").Value;
                    string subKey       = registryWatcher.Element("SubKey").Value;
                    string regItem      = registryWatcher.Element("RegItem").Value;
                    string regType      = registryWatcher.Element("RegType").Value;
                    string regItemValue = registryWatcher.Element("RegItemValue").Value;

                    RegistryValueKind regValueKind = RegistryValueKind.String;

                    //string baseRegistryKey = "HKEY_USERS";
                    //string subKey = "Software\\Microsoft\\Office\\15.0\\Lync\\[email protected]\\Autodiscovery";
                    //string regItem = "InternalEcpUrl";
                    //string regItemValue = @"https://mail.lynconline.co.nz/ecp/?rfr=olk&p=customize/voicemail.aspx&exsvurl=1&realm=lynconline.co.nz";
                    //string regItemValue = @"TEST1";
                    //MessageBox.Show("Watcher: " + baseRegistryKey + " :: " + registryView + " :: " + subKey + " :: " + regItem + " :: " + regItemValue);

                    //Monitor InternalEcpUrl
                    //InternalEcpUrl.BaseRegistryKey = Registry.CurrentUser;
                    RegMon regMon = new RegMon();

                    //Registry View
                    if (registryView == "Registry32")
                    {
                        regMon.RegistryView = RegistryView.Registry32;
                    }
                    else if (registryView == "Registry64")
                    {
                        regMon.RegistryView = RegistryView.Registry64;
                    }

                    //Registry Value kind
                    if (regType.ToLower() == "string")
                    {
                        regValueKind = RegistryValueKind.String;
                    }
                    else if (regType.ToLower() == "binary")
                    {
                        regValueKind = RegistryValueKind.Binary;
                    }
                    else if (regType.ToLower() == "dword")
                    {
                        regValueKind = RegistryValueKind.DWord;
                    }
                    else if (regType.ToLower() == "qword")
                    {
                        regValueKind = RegistryValueKind.QWord;
                    }

                    //Registry Hive
                    if (registryHive == "CurrentUser")
                    {
                        regMon.RegistryHive = RegistryHive.CurrentUser;
                    }
                    else if (registryHive == "LocalMachine")
                    {
                        regMon.RegistryHive = RegistryHive.LocalMachine;

                        //Prompt to elevate if required
                        UserAccessControl uacRegWatcher = new UserAccessControl();

                        if (!uacRegWatcher.IsRunAsAdmin())
                        {
                            if (MessageBox.Show("The application does not have the required permissions to monitor the LocalMachine registry hive, please try running as administrator or check your permissions.", "Registry Watcher", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                            {
                                uacRegWatcher.Elevate();
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                    //MessageBox.Show(subKey + regItem + regItemValue + RegistryValueKind.String);
                    regMon.Monitor(subKey, regItem, regItemValue, regValueKind);

                    ////Read registry key
                    //regMod.BaseRegistryKey = Registry.CurrentUser;
                    //regMod.SubKey = "Software\\Microsoft\\Office\\15.0\\Lync\\[email protected]\\Autodiscovery";
                    //var x = regMod.Read("InternalEcpUrl");
                    //MessageBox.Show(x);
                }
            }
            catch (Exception e)
            {
                // AAAAAAAAAAARGH, an error!
                Shared.MB("Error monitoring regisrty for changes : " + e.Message, "ERROR!");
            }
        }