创建时为文件夹提供完全访问权

时间:2013-02-13 12:01:32

标签: vb.net

我有一个情况,我有一个Windows应用程序将在服务器上创建文件夹,如果它不存在然后我将文件复制到它但目前我在指定的路径上被拒绝访问。这就是我所拥有的。

If (Not System.IO.Directory.Exists(sdPath & "\DONE")) Then
   System.IO.Directory.CreateDirectory(sdPath & "\DONE")
   sdFileInfo.MoveTo(sdPath & "\DONE\" & sdFileName & sdFileInfo.Extension)
Else
   sdFileInfo.MoveTo(sdPath & "\DONE\" & sdFileName & sdFileInfo.Extension)
End If

如何授予完全访问权限?

我有这段测试代码,看看我是否可以获得权限,但似乎无法正常工作。

txtPath.Text = "\\ServerName\images\dbpics\POLPICS\55\Polpics\Actual\"

Dim sFolderPath As String = txtPath.Text
Dim sUserAccount As String = "(DomainName\Administrator)"

Dim oFolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(sFolderPath)
Dim oFolderAcl As New DirectorySecurity(txtPath.Text, System.Security.AccessControl.AccessControlSections.Access)

oFolderAcl.AddAccessRule(New FileSystemAccessRule(sUserAccount, _
                                                  FileSystemRights.FullControl, _
                                                  AccessControlType.Allow))

oFolderInfo.SetAccessControl(oFolderAcl)

oFolderAcl = Nothing
oFolderInfo = Nothing
enter code here

2 个答案:

答案 0 :(得分:3)

尝试使用:

Imports System.Security.AccessControl


 Dim FolderPath As String = "C:\TestingFolder" 'Specify the folder here
 Dim UserAccount As String = "MYDOMAIN\someuser" 'Specify the user here

 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))
'FolderAcl.SetAccessRuleProtection(True, False) 'uncomment to remove existing permissions
FolderInfo.SetAccessControl(FolderAcl)

我从URL

获得了它

答案 1 :(得分:2)

您可以将所需的安全性添加到创建它的目录:

System.IO.Directory.CreateDirectory(sFolderPath, dirSecurity)

您可以在MSDN的CreateDirectoryDirectorySecurity的文档中看到一些权限和示例。