[英]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.