コード例 #1
0
        private async void OnShellMenuItemInvoke(object sender, ShellMenuInvokeEventArgs e)
        {
            // e.MenuItem can be null for standard commands
            var menu = (ShellMenu)sender;

            // log something (if a logger is configured)
            menu.Server.Configuration?.Logger?.Log(TraceLevel.Info, "Shell Item '" + e.MenuItem + "' (cmd:" + e.Command + ") called.");

            var mc = Conversions.ChangeType(e.MenuItem?.Tag, MenuCommand.Unknown);

            switch (mc)
            {
            case MenuCommand.Modify:
                if (e.Items.Count == 1)                // we only support modification of one value at a time
                {
                    using (var form = new EditValue()) // note because of async + await, Dispose will happen in continuing task
                    {
                        var valueItem = (RegistryValueItem)e.Items[0];
                        form.LoadEditor(BaseParent.Hive, Path, valueItem.KeyName);
                        await WindowsUtilities.ShowModelessAsync(form, e.HwndOwner).ContinueWith((task) =>
                        {
                            if (task.Result == DialogResult.OK)
                            {
                                using (var key = OpenKey(true))
                                {
                                    key.SetValue(valueItem.KeyName, form.NewValue);
                                    valueItem.Parent?.NotifyUpdate();
                                }
                            }
                        });
                    }
                    return;
                }
                break;

            case MenuCommand.NewKey:
                using (var key = OpenKey(true))
                {
                    if (key != null)
                    {
                        var newName = GetNewName("New Key #", key.GetSubKeyNames());
                        try
                        {
                            key.CreateSubKey(newName);
                            e.Folder.RefreshShellViews();
                            await SelectAndEdit(newName);
                        }
                        catch (Exception ex)
                        {
                            // probably an access denied error
                            await WindowsUtilities.DoModelessAsync(() =>
                            {
                                MessageBox.Show(new Win32Window(e.HwndOwner), "The Registry Folder cannot set a value here: " + ex.Message, "Registry Folder", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            });
                        }
                    }
                }
                break;

            case MenuCommand.NewValueString:
            case MenuCommand.NewValueBinary:
            case MenuCommand.NewValueDWord:
            case MenuCommand.NewValueQWord:
            case MenuCommand.NewValueMultiString:
            case MenuCommand.NewValueExpandString:
                var kind = (RegistryValueKind)mc;
                using (var key = OpenKey(true))
                {
                    if (key != null)
                    {
                        var newName = GetNewName("New Value #", key.GetValueNames());
                        try
                        {
                            key.SetValue(newName, GetDefaultValue(kind), kind);
                            e.Folder.RefreshShellViews();
                            await SelectAndEdit(newName);
                        }
                        catch (Exception ex)
                        {
                            // probably an access denied error
                            await WindowsUtilities.DoModelessAsync(() =>
                            {
                                MessageBox.Show(new Win32Window(e.HwndOwner), "The Registry Folder cannot set a value here: " + ex.Message, "Registry Folder", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            });
                        }
                    }
                }
                break;
            }
        }