How to enable the SeCreateGlobalPrivilege in .NET without resorting to P/Invoke or reflection?
See the question and my original answer on StackOverflowIf you don't want to use P/Invoke by yourself, you can still use the internal System.Security.AccessControl.Privilege
class, using Reflection mechanisms, like this:
// => privilege = new Privilege("SeCreateGlobalPrivilege");
// this is for .NET Framework
var privilegeType = Type.GetType("System.Security.AccessControl.Privilege");
// this for .NET Core and above (you need to add the "System.Security.AccessControl" nuget package)
var privilegeType = Type.GetType("System.Security.AccessControl.Privilege, System.Security.AccessControl");
var privilege = Activator.CreateInstance(privilegeType, "SeCreateGlobalPrivilege");
// => privilege.Enable();
privilegeType.GetMethod("Enable").Invoke(privilege, null);
// => privilege.Revert();
privilegeType.GetMethod("Revert").Invoke(privilege, null);
I'm not sure it's really better, because it's more or less a hack, not supported et al., but ok, it's easy for lazy guys :-)