From cb4061a6baf5d6db1adf62db40b2a0939d7b4451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E7=82=B9lilac?= Date: Wed, 30 Dec 2020 20:14:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=89=80=E6=9C=89=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/FileExtensionDialog.cs | 63 ++++--------------- 1 file changed, 11 insertions(+), 52 deletions(-) diff --git a/ContextMenuManager/Controls/FileExtensionDialog.cs b/ContextMenuManager/Controls/FileExtensionDialog.cs index f0f39ae..4396129 100644 --- a/ContextMenuManager/Controls/FileExtensionDialog.cs +++ b/ContextMenuManager/Controls/FileExtensionDialog.cs @@ -7,7 +7,6 @@ namespace ContextMenuManager.Controls { sealed class FileExtensionDialog : CommonDialog { - const string FileExtsPath = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts"; public string Extension { get; private set; } public override void Reset() { } @@ -21,37 +20,6 @@ namespace ContextMenuManager.Controls } } - public static string GetTypeName(string extension, bool includeUWP = true) - { - using(var root = Microsoft.Win32.Registry.ClassesRoot) - { - bool TypeNameExists(string typeName) - { - if(!string.IsNullOrEmpty(typeName)) - using(var typeKey = root.OpenSubKey(typeName)) - if(typeKey != null) return true; - return false; - } - - using(var extKey = root.OpenSubKey(extension)) - { - if(extKey == null) return null; - string defaultType = extKey.GetValue("")?.ToString(); - if(TypeNameExists(defaultType)) return defaultType; - using(var key = extKey.OpenSubKey("OpenWithProgids")) - { - if(key == null) return null; - foreach(string valueName in key.GetValueNames()) - { - if(!includeUWP && key.GetValueKind(valueName) != Microsoft.Win32.RegistryValueKind.String) continue; - if(TypeNameExists(valueName)) return valueName; - } - } - } - } - return null; - } - sealed class FileExtensionForm : Form { public FileExtensionForm() @@ -68,16 +36,9 @@ namespace ContextMenuManager.Controls LoadExtensions(); btnOk.Click += (sender, e) => { - if(cmbExtension.Items.Contains(cmbExtension.Text)) - { - this.Extension = $".{cmbExtension.Text}"; - this.DialogResult = DialogResult.OK; - } - else - { - MessageBoxEx.Show(AppString.MessageBox.UnsupportedExtension); - cmbExtension.Focus(); - } + int index = cmbExtension.Text.IndexOf('.'); + if(index >= 0) this.Extension = cmbExtension.Text.Substring(index); + else this.Extension = $".{cmbExtension.Text}"; }; } @@ -87,10 +48,12 @@ namespace ContextMenuManager.Controls { AutoCompleteMode = AutoCompleteMode.SuggestAppend, AutoCompleteSource = AutoCompleteSource.ListItems, - DropDownHeight = 294.DpiZoom() + DropDownHeight = 294.DpiZoom(), + ImeMode = ImeMode.Disable }; readonly Button btnOk = new Button { + DialogResult = DialogResult.OK, Text = AppString.Dialog.Ok, AutoSize = true }; @@ -103,26 +66,22 @@ namespace ContextMenuManager.Controls private void InitializeComponents() { - this.ClientSize = new Size(316, 110).DpiZoom(); this.Controls.AddRange(new Control[] { cmbExtension, btnOk, btnCancel }); int a = 20.DpiZoom(); cmbExtension.Left = a; cmbExtension.Width = 85.DpiZoom(); - cmbExtension.Top = btnOk.Top = btnCancel.Top = 2 * a; + cmbExtension.Top = btnOk.Top = btnCancel.Top = a; btnOk.Left = cmbExtension.Right + a; btnCancel.Left = btnOk.Right + a; + this.ClientSize = new Size(btnCancel.Right + a, btnCancel.Bottom + a); } private void LoadExtensions() { - using(var extKey = RegistryEx.GetRegistryKey(FileExtsPath)) + foreach(string extension in Microsoft.Win32.Registry.ClassesRoot.GetSubKeyNames()) { - if(extKey == null) return; - foreach(string extension in extKey.GetSubKeyNames()) - { - if(!extension.StartsWith(".") || GetTypeName(extension) == null) continue; - cmbExtension.Items.Add(extension.Substring(1)); - } + if(!extension.StartsWith(".")) continue; + cmbExtension.Items.Add(extension.Substring(1)); } } }