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);
}
}
}
}