我试图找出用户是否已经写过'对文件夹的权限。从我所做的所有搜索来看,它似乎比它应该更复杂。我在我的C:驱动器上创建了一个只读文件夹,名为" NoWrite'并试图在VB(VS 2013)中获取正确的访问信息。我尝试过使用' Permission.Demand'在文件夹上,它没有错误。我尝试了“目录安全”#39;并且它说我有访问权限,但是当我尝试编写文件时,它会因访问错误而出错。我很沮丧......谁能看到我做错了什么?
[编辑]我正在通过此代码运行6个不同的文件夹。 {folderName} \ aTestFile.txt正在我所拥有的5个文件夹中创建'写'访问,所以我知道它正在工作。
这是我的代码:
Dim fldrInfo As New DirectoryInfo(FolderToCheck)
Dim ds As System.Security.AccessControl.DirectorySecurity = fldrInfo.GetAccessControl
Dim fs As System.Security.AccessControl.AuthorizationRuleCollection = ds.GetAccessRules(True, True, Type.GetType("System.Security.Principal.NTAccount"))
Dim wid As Principal.WindowsIdentity = Principal.WindowsIdentity.GetCurrent
For Each myAcc As System.Security.AccessControl.AccessRule In fs
Dim acc As System.Security.AccessControl.FileSystemAccessRule = DirectCast(myAcc, System.Security.AccessControl.FileSystemAccessRule)
If acc.IdentityReference.Value.ToUpper = wid.Name.ToUpper Then
Debug.Print("Folder To Check = " & FolderToCheck)
Debug.Print("Windows User ID = " & wid.Name)
Debug.Print("Access Control Identity = " & acc.IdentityReference.ToString)
Debug.Print("File System Rights = " & acc.FileSystemRights.ToString)
Debug.Print("Access Control Type = " & acc.AccessControlType.ToString)
Debug.Print("")
If (acc.FileSystemRights And AccessControl.FileSystemRights.WriteData) = AccessControl.FileSystemRights.WriteData Then
Try
Dim myDs As New FileStream(FolderToCheck & "\aTestFile.txt", FileMode.OpenOrCreate)
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End If
End If
Next
这是输出:
要检查的文件夹= C:\ NoWrite
Windows用户ID = KARL-HP \ Karl
访问控制标识= Karl-HP \ Karl
文件系统权限= FullControl
访问控制类型=允许
访问路径' C:\ NoWrite \ aTestFile.txt'被拒绝。