Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I want to remove the sections from the registry System\CurrentControlSet\Enum\USB, but i cant delete PROPERTIES (windows10)

string user = Environment.UserDomainName + "\\" + Environment.UserName;
RegistrySecurity mSec = new RegistrySecurity();
RegistryAccessRule newRule =
new RegistryAccessRule(
user,
RegistryRights.FullControl,
InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
PropagationFlags.None,
AccessControlType.Allow);

RegistryKey hkusb = hklm.OpenSubKey(RegistryPath, RegistryKeyPermissionCheck.ReadSubTree);

RegistrySecurity security = hkusb.GetAccessControl();
if (ContainsRule(security.GetAccessRules(true, true, typeof(SecurityIdentifier)), newRule))
{
Console.WriteLine("Access");
Console.ReadKey();
return;
}
security.AddAccessRule(newRule);

hkusb.SetAccessControl(security); <----error here
hkusb.Close();
showKeyTree(hkusb, "+");
Console.ReadKey();



this code allows me to delete all sections exept PROPERTIES

i understand that iam need to get a system permissions to do this, but i dont know how to make it in code.
Posted

This is how you can start the application with elevated permissions:
https://msdn.microsoft.com/en-us/library/0w4h05yb%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.verb%28v=vs.110%29.aspx[^].

The key here is using Verb; for permission elevation, it should be equal to the string "runas". Unfortunately, this is not so well documented in MSDN.

Another approach is to create and embed application manifest which would require UAC request from the very beginning. Please see this Microsoft article: http://msdn.microsoft.com/en-us/library/bb756929.aspx[^].

—SA
 
Share this answer
 
You need to run your program as administrator.
If you want to do that using Visual Studio, you start VS by right-clicking and select "Run as administrator".

If you run your program stand alone, you start your program the same way.

For doing it in code, see Getting Elevated Privileges on Demand using C#[^] and How to self-elevate an application to a high privilege level under UAC[^]
 
Share this answer
 
v2
Comments
frostazaza 25-Oct-15 15:53pm    
I need to start process as system
like PsExec, but in code
George Jonsson 25-Oct-15 16:05pm    
See my updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900