C# 如何在Windows 8.1中将SeSecurityPrivilege添加到目录?

C# 如何在Windows 8.1中将SeSecurityPrivilege添加到目录?,c#,process,windows-8.1,rsa,privilege,C#,Process,Windows 8.1,Rsa,Privilege,我想将SeSecurityPrivilege添加到目录中 我找到了那个图书馆。 我在Windows8.1中测试了下面的代码 static void Main(string[] args) { String dirPath = @"C:\Users\İsmail\AppData\Roaming\Microsoft\Crypto\RSA\S-1-5-21-456447968-1492996402-1718433538-1001"; Process process = Process

我想将SeSecurityPrivilege添加到目录中

我找到了那个图书馆。

我在Windows8.1中测试了下面的代码

static void Main(string[] args)
{
    String dirPath = @"C:\Users\İsmail\AppData\Roaming\Microsoft\Crypto\RSA\S-1-5-21-456447968-1492996402-1718433538-1001";
    Process process = Process.GetCurrentProcess();

    // Get the privileges and associated attributes.
    PrivilegeAndAttributesCollection privileges = process.GetPrivileges();

    using (new ProcessPrivileges.PrivilegeEnabler(process, Privilege.Security))
    {
        // Privilege is enabled within the using block.

        DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
        DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
        directorySecurity.SetOwner(WindowsIdentity.GetCurrent().User);
        Directory.SetAccessControl(dirPath, directorySecurity);

        privileges = process.GetPrivileges();
        PrivilegeState state = process.GetPrivilegeState(Privilege.Security);


        DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
        DirectorySecurity DirSec = dirInfo.GetAccessControl(AccessControlSections.All);

    }
}
下面的代码行引发异常

DirectorySecurity DirSec = dirInfo.GetAccessControl(AccessControlSections.All);
异常结果:

System.Security.AccessControl.PrivilegeNotHeldException was unhandled
  HResult=-2147024891
  Message=The process does not possess the 'SeSecurityPrivilege' privilege which is required for this operation.
  Source=mscorlib
  PrivilegeName=SeSecurityPrivilege
  StackTrace:
       at System.Security.AccessControl.Win32.GetSecurityInfo(ResourceType resourceType, String name, SafeHandle handle, AccessControlSections accessControlSections, RawSecurityDescriptor& resultSd)
       at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)
       at System.Security.AccessControl.NativeObjectSecurity..ctor(Boolean isContainer, ResourceType resourceType, String name, AccessControlSections includeSections, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)
       at System.Security.AccessControl.DirectorySecurity..ctor(String name, AccessControlSections includeSections)
       at System.IO.DirectoryInfo.GetAccessControl(AccessControlSections includeSections)
       at TestACLS.Program.Main(String[] args) in c:\Users\İsmail\Desktop\TestACLS\TestACLS\TestACLS\Program.cs:line 72
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

您的意思是要为您的进程启用SeSecurityPrivilege,以便可以从目录中读取SACL吗?您是否记得以管理员身份运行进程?谢谢您的评论。我不明白我在Windows7中测试的安全特权,它可以工作。但仅在windows 8.1下不起作用我在windows 7上用whoami/priv命令检查了SeSecurityPrivilege。我看到SeSecurityPrivilege状态已禁用。我在Windows8.1上用whoami/priv命令检查了SeSecurityPrivilege。我没有看到任何SeSecurityPrivilege。同样,运行Windows 7时,Windows 8.1也不能正常工作,因为。只有在运行提升版时,您才应该拥有SeSecurityPrivilege。我怀疑您已在Windows 7计算机上关闭了UAC。