vb.net 创建时授予文件夹完全访问权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14853105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Give folder full access when created
提问by Gericke
I have a situation, I have a windows app that will create folders on a server if it does not exists and then I will copy a file to it but currently I get access denied on the specified path. This is what I have.
我有一个情况,我有一个 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
How can I give Full access to?
我如何授予完全访问权限?
I have this piece of test code to see if I can get the permissions but doesn't seem to work.
我有这段测试代码,看看我是否可以获得权限,但似乎不起作用。
txtPath.Text = "\ServerName\images\dbpics\POLPICS\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
回答by Jacques Bronkhorst
Try using:
尝试使用:
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)
I got it from this URL
我是从这个网址得到的
回答by SysDragon
You can add the security you want to the directory creating it with:
您可以将所需的安全性添加到创建它的目录中:
System.IO.Directory.CreateDirectory(sFolderPath, dirSecurity)
You can see some permissions and examples in the documentation of the CreateDirectoryand the DirectorySecurityof MSDN.
您可以在MSDN的CreateDirectory和DirectorySecurity的文档中看到一些权限和示例。

