如何从 c-sharp 的注册表中删除 ApplicationAssociationToasts 子项

问题描述 投票:0回答:1

我想删除

HKEY_CURRENT_USER
>
Software
>
Microsoft
>
Windows
>
CurrentVersion
>
ApplicationAssociationToasts
>
Applications\devenv.exe_.svg
。为此,我尝试了这段代码:

var np = @"Software\Microsoft\Windows\CurrentVersion\ApplicationAssociationToasts\" + $@"Applications\notepad++.exe_.svg";
Registry.CurrentUser.DeleteSubKey(np, false);

这是截图:

但它不起作用。

c# registry
1个回答
0
投票

使用.NET Framework 中的RegistryKey 类,您可以在C# 中从注册表中删除子项。更改注册表时请务必小心,因为错误的更改可能会导致应用程序或整个系统出现故障。在进行任何更改之前,请确保您有注册表的备份。

这是从注册表中删除 ApplicationAssociationToasts 子项的简单示例。本示例中的子项假定位于 HKEY_CURRENT_USER\Software\ExampleSubkey 中。应使用 ApplicationAssociationToasts 子项的路径代替 ExampleSubkey。

using System;
using Microsoft.Win32;
class Program
{
    static void Main()
    {
        // Define the path to the parent key of the subkey you want to delete.
        // In this example, "Software\ExampleSubkey" is the parent key of "ApplicationAssociationToasts".
        string parentKeyPath = @"Software\ExampleSubkey";

        try
        {
            // Open the parent key with write access.
            using (RegistryKey parentKey = Registry.CurrentUser.OpenSubKey(parentKeyPath, writable: true))
            {
                if (parentKey == null)
                {
                    Console.WriteLine("The specified parent key does not exist.");
                }
                else
                {
                    // Delete the "ApplicationAssociationToasts" subkey.
                    parentKey.DeleteSubKey("ApplicationAssociationToasts");
                    Console.WriteLine("Subkey 'ApplicationAssociationToasts' was successfully deleted.");
                }
            }
        }
        catch (Exception ex)
        {
            // An exception can occur if the subkey does not exist or you do not have the necessary permissions.
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
}

在执行此代码之前,请确保:

您有权更改注册表。使用管理权限运行您的应用程序可能很重要。 已准确确定要删除的子项的完整路径。确保您使用的是注册表中正确的根键,因为有很多根键(例如 HKEY_CURRENT_USER 和 HKEY_LOCAL_MACHINE)。 您了解删除子项后会发生什么。某些应用程序的设置和功能可能需要注册表项。 与往常一样,最好在非关键环境或系统上测试修改,以防止出现不可预见的问题。

© www.soinside.com 2019 - 2024. All rights reserved.