【问题标题】:SetAccessControl giving error as An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dllSetAccessControl 给出错误为 mscorlib.dll 中发生“System.UnauthorizedAccessException”类型的未处理异常
【发布时间】:2016-10-01 14:27:57
【问题描述】:

我需要设置一个文件夹的访问控制

我的代码是

 Private Sub cmdApplyRestrictions_Click(sender As Object, e As EventArgs) Handles cmdApplyRestrictions.Click
    Dim myDirectoryInfo As New DirectoryInfo(txtFolder.Text)

    Dim myDirectorySecurity As DirectorySecurity = myDirectoryInfo.GetAccessControl()
    Dim User As String = System.Environment.UserDomainName + "\" + cmbUser.SelectedItem.ToString()

    myDirectorySecurity.AddAccessRule(New FileSystemAccessRule(User, FileSystemRights.Read, AccessControlType.Deny))
    myDirectoryInfo.SetAccessControl(myDirectorySecurity)
    MessageBox.Show("Permissions Altered Successfully")

End Sub 

一行

myDirectoryInfo.SetAccessControl(myDirectorySecurity)

作为

给出异常

在 mscorlib.dll 中发生了“System.UnauthorizedAccessException”类型的未处理异常

我以具有管理权限但不是管理员本身的用户身份登录 我需要阻止所有用户的访问,包括登录用户以及包括管理员在内的所有用户

等我的程序结束后,我会恢复权限

还有一个要求是我希望向一个外部程序授予对该文件夹的访问权限

【问题讨论】:

  • 如果您使用的是 Windows,则不能“向一个外部程序授予对该文件夹的访问权限”,因为进程本身没有权限。 (可能想澄清您在问题中使用非 Windows 操作系统的事实)
  • 我使用的是 Windows 8

标签: c# vb.net permissions directory access-control


【解决方案1】:

您需要以提升的权限执行您的应用程序。

使用此内容将 app.manifest 文件添加到您的应用中:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</asmv1:assembly>

【讨论】:

  • 当你执行它时,你的应用会要求你提供管理员权限?
  • 那你做错了,它必须要求管理员权限,删除文件,重新添加(添加->应用程序清单)并设置我粘贴的确切内容。
  • 当我尝试运行 exe 时,它​​说 A refer was returned from the server and doesn't run
  • 谢谢,它成功了,但是你对我的完整要求有任何线索吗,我需要阻止除程序之外的所有对该文件夹的访问
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多