0

I need to delete information about usb flash drive in SYSTEM\ControlSet001\Enum\USB. But I can't do it. Apparently you need system rights, how can I get them for the application? Code below

string explorerKeyPath = @ "SYSTEM\ControlSet001\Enum\USB";

using (RegistryKey explorerKey =
        Registry.LocalMachine.OpenSubKey(explorerKeyPath, writable: true)) {
        
    if (explorerKey != null) {
        var subKeys = explorerKey.GetSubKeyNames();
        
        foreach(string s in subKeys) {
            string tmpStr = s.ToLower();
            tmpStr = tmpStr.Split('&').First();
            string selectedDeviceTmpStr = SelectedUSBDevice.PnpDeviceID.ToLower().Split('&').First();
            
            if (tmpStr == selectedDeviceTmpStr) {
                using(RegistryKey tempKey = explorerKey.OpenSubKey(s)) {
                    var valueSKNames = tempKey.GetSubKeyNames();
                    
                    foreach(var tmpValName in valueSKNames) {
                        using(RegistryKey tempKey1 = tempKey.OpenSubKey(tmpValName)) {

                            var valueNames = tempKey1.GetValueNames();
                            var selectedValName = valueNames.Select(x => x).Where(y => y == "ContainerID").FirstOrDefault();

                        }
                        
                        tempKey.DeleteSubKeyTree(tmpValName);
                    }
                    
                    tempKey.Close();
                }
                
                Debug.WriteLine($"{explorerKeyPath}\\{s}");
                explorerKey.DeleteSubKey(s);
            }
        }
    }
}
Konrad Rudolph
  • 506,650
  • 124
  • 909
  • 1,183
  • 1
    Does this answer your question? [Elevating process privilege programmatically?](https://stackoverflow.com/questions/133379/elevating-process-privilege-programmatically) – alagner Oct 19 '21 at 11:57

0 Answers0