繁体   English   中英

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

[英]How do I remove the keys from Local machine\SOFTWARE?

我试图通过C#脚本删除测试键。 以下代码是我的脚本,我还在此项目UAC的清单文件中添加了admin值。 但这仍然行不通。 甚至以Admin模式重启了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();
        }

    }
  }
}

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

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

它应该像这样工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM