如何从本地机器\软件中删除密钥?

Posted

技术标签:

【中文标题】如何从本地机器\\软件中删除密钥?【英文标题】:How do I remove the keys from Local machine\SOFTWARE?如何从本地机器\软件中删除密钥? 【发布时间】:2019-03-27 14:20:39 【问题描述】:

我尝试通过 C# 脚本删除测试密钥。以下代码是我的脚本,我还在此项目 UAC 的清单文件中添加了管理值。但它仍然不起作用。甚至以管理员模式重新启动 Visual Studio 2017。 错误消息说无法写入注册表项。 不知道脚本有什么问题。

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Security;
using System.Security.AccessControl;
using System.Security.Principal;


namespace temp_code_tester

class Program

    static void Main(string[] args)
    
        //Get current login account info from another Class
        //string userName = WindowsIdentity.GetCurrent().Name;
        //var SecCheck = new SecutriyProcesser();
        //SecCheck.AddRule(userName);


        var RunCheck = new AccessRegistry();
        RunCheck.ACL("Test");

    


class AccessRegistry

    public void ACL(string name)
    
        Console.WriteLine("Getting the registry keys.....");
        Console.WriteLine("---------------------------------------------\n");

        //Open the SOFTWARE keys and input those keys into array
        RegistryKey SoftKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE");
        string[] lists = SoftKey.GetSubKeyNames();

        foreach (string KeyName in lists)
        
            Console.WriteLine(KeyName);
        

        foreach (string value in lists)
        
            if (value.Contains(name)) // if we find the key, then lists all subkeys
            
                //Registry.LocalMachine.DeleteSubKeyTree(value);
                Console.WriteLine("\nMatch one: 0", value);

                var RightKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\" + value);
                string[] SubList = RightKey.GetSubKeyNames();

                foreach (string SubValue in SubList)
                
                    Console.WriteLine("Folder: 0 is under this key", SubValue);
                

                if (SubList.Length > 1)
                
                    try
                    
                        SoftKey.DeleteSubKeyTree(value);
                        Console.WriteLine("\nThe folder 0 has been removed", value);
                    
                    catch (Exception er)
                    
                        Console.WriteLine("Error: 0", er.Message);
                    
                
                else
                
                    try
                    
                        SoftKey.DeleteSubKey(value);
                    
                    catch (Exception)
                    
                        throw;
                    

                
            
        
        SoftKey.Close();
    


class SecutriyProcesser

    // A method about add enough roles for current window account
    public void AddRule(string userName)
    
        RegistrySecurity rs = new RegistrySecurity();
        rs.AddAccessRule(new RegistryAccessRule(userName,
            RegistryRights.FullControl,
            InheritanceFlags.ObjectInherit,
            PropagationFlags.InheritOnly,
            AccessControlType.Allow));
    

    // Try to list the security level
    public void ShowSecurity(RegistrySecurity security)
    
        Console.WriteLine("\r\nCurrent access rules:\r\n");

        foreach (RegistryAccessRule ar in security.GetAccessRules(true, true, typeof(NTAccount)))
        

            Console.WriteLine("        User: 0", ar.IdentityReference);
            Console.WriteLine("        Type: 0", ar.AccessControlType);
            Console.WriteLine("      Rights: 0", ar.RegistryRights);
            Console.WriteLine(" Inheritance: 0", ar.InheritanceFlags);
            Console.WriteLine(" Propagation: 0", ar.PropagationFlags);
            Console.WriteLine("   Inherited? 0", ar.IsInherited);
            Console.WriteLine();
        

    
  

【问题讨论】:

【参考方案1】:

您忘记了“OpenSubkey”方法的“可写”参数。 试试这个:

RegistryKey SoftKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE", true);

它应该像这样工作。

编辑:如果您在调试时不以管理员身份运行 Visual Studio,此方法可能会失败。

【讨论】:

以上是关于如何从本地机器\软件中删除密钥?的主要内容,如果未能解决你的问题,请参考以下文章

Docker TLS - 如何在本地机器上创建密钥

如何从 Windows 机器中完全删除 Python?

删除 SSH 密钥终端

如何在 git push 之前从 Android 项目中删除 API 密钥

如何在IIS上创建机器密钥?

如何以编程方式从 .NET 中的注册表中删除 Windows 产品密钥?