mirror of
https://github.com/BluePointLilac/ContextMenuManager.git
synced 2026-01-14 06:04:00 +08:00
支持所有扩展名
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user