如何设置文件夹写访问权限

时间:2018-03-13 20:52:52

标签: vb.net permissions directory basic

继承我的代码

Dim FolderPath As String = "C:/folder"
Dim UserAccount As String = "(DESKTOP-T34R8TH\Users)"
Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FolderPath)
Dim FolderAcl As New DirectorySecurity
FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
FolderInfo.SetAccessControl(FolderAcl)
没有任何反应。我希望该文件夹具有写入权限。

1 个答案:

答案 0 :(得分:0)

您无法将新的DirectorySecurity对象添加到该文件夹​​。您必须检索现有的并修改它。

所以不要使用

Dim FolderAcl As New DirectorySecurity

你应该使用

Dim FolderAcl As DirectorySecurity = FolderInfo.GetAccessControl()

有关综合示例,请参阅MS文档here