在 C# 中从注册表中删除与文件扩展名关联的图标
Deleting the icon associated with the file extension from the registry in C#
我使用这样的方法将文件扩展名与图标相关联:
public static void Associate(string extension, string progID, string description, string icon, string application)
{
Registry.ClassesRoot.CreateSubKey(extension)?.SetValue("", progID);
if (!string.IsNullOrEmpty(progID))
{
using var key = Registry.ClassesRoot.CreateSubKey(progID);
if (description != null)
key?.SetValue("", description);
if (icon != null)
key?.CreateSubKey("DefaultIcon")?.SetValue("", ToShortPathName(icon));
if (application != null)
key?.CreateSubKey(@"Shell\Open\Command")?.SetValue("", ToShortPathName(application) + " \"%1\"");
}
}
当我想彻底删除关联的文件扩展名时,我不知道如何进行。为此,我尝试了这样的事情:
public static void Remove(string progID)
{
Registry.ClassesRoot.DeleteSubKey(progID);
}
它给出了这样的错误:
The registry has subkeys and recursive removals are not supported by this method.
在这个地址有解决方案:https://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C
像这样:
public static RegistryKey BaseRegistryKey { get; set; } = Registry.LocalMachine;
public static string subKey { get; set; } = "SOFTWARE\" + Application.ProductName.ToUpper();
public bool DeleteSubKeyTree()
{
try
{
// Setting
RegistryKey rk = baseRegistryKey ;
RegistryKey sk1 = rk.OpenSubKey(subKey);
// If the RegistryKey exists, I delete it
if ( sk1 != null )
rk.DeleteSubKeyTree(subKey);
return true;
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
MessageBox.Show(e, "Deleting SubKey " + subKey);
return false;
}
}
我使用这样的方法将文件扩展名与图标相关联:
public static void Associate(string extension, string progID, string description, string icon, string application)
{
Registry.ClassesRoot.CreateSubKey(extension)?.SetValue("", progID);
if (!string.IsNullOrEmpty(progID))
{
using var key = Registry.ClassesRoot.CreateSubKey(progID);
if (description != null)
key?.SetValue("", description);
if (icon != null)
key?.CreateSubKey("DefaultIcon")?.SetValue("", ToShortPathName(icon));
if (application != null)
key?.CreateSubKey(@"Shell\Open\Command")?.SetValue("", ToShortPathName(application) + " \"%1\"");
}
}
当我想彻底删除关联的文件扩展名时,我不知道如何进行。为此,我尝试了这样的事情:
public static void Remove(string progID)
{
Registry.ClassesRoot.DeleteSubKey(progID);
}
它给出了这样的错误:
The registry has subkeys and recursive removals are not supported by this method.
在这个地址有解决方案:https://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C
像这样:
public static RegistryKey BaseRegistryKey { get; set; } = Registry.LocalMachine;
public static string subKey { get; set; } = "SOFTWARE\" + Application.ProductName.ToUpper();
public bool DeleteSubKeyTree()
{
try
{
// Setting
RegistryKey rk = baseRegistryKey ;
RegistryKey sk1 = rk.OpenSubKey(subKey);
// If the RegistryKey exists, I delete it
if ( sk1 != null )
rk.DeleteSubKeyTree(subKey);
return true;
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
MessageBox.Show(e, "Deleting SubKey " + subKey);
return false;
}
}