none
Directory.SetAccessControl not setting permissions

    Pertanyaan

  • I'm trying to set ACL's on a folder using Directory.SetAccessControl.  More specifically, I want to grant "Users" full control on a folder.  AddFileSecurity works perfectly.  AddFolderSecurity does not.  It's result is that "Users" will get added to the folder's ACL but "Full Control" is not checked off (nor are any other permissions).  Here is the code I am using which is basically taken right from MSDN documentation (http://msdn.microsoft.com/en-us/library/system.io.directory.setaccesscontrol.aspx ): 

    Imports System.IO
    Imports System.Security.AccessControl
    
    Module Module1
    
      Sub Main()
        AddFolderSecurity("C:\TestFolder", "Users", FileSystemRights.FullControl, AccessControlType.Allow)
        AddFileSecurity("C:\TestFile.txt", "Users", FileSystemRights.FullControl, AccessControlType.Allow)
      End Sub
    
      ' Adds an ACL entry on the specified file for the specified account.
      Sub AddFileSecurity(ByVal fileName As String, ByVal account As String, _
        ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)
    
        ' Get a FileSecurity object that represents the 
        ' current security settings.
        Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)
    
        ' Add the FileSystemAccessRule to the security settings. 
        Dim accessRule As FileSystemAccessRule = _
          New FileSystemAccessRule(account, rights, controlType)
    
        fSecurity.AddAccessRule(accessRule)
    
        ' Set the new access settings.
        File.SetAccessControl(fileName, fSecurity)
    
      End Sub
    
      ' Adds an ACL entry on the specified file for the specified account.
      Sub AddFolderSecurity(ByVal folderName As String, ByVal account As String, _
        ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)
    
        ' Get a FileSecurity object that represents the 
        ' current security settings.
        'Dim fSecurity As DirectorySecurity = Directory.GetAccessControl(folderName)
        Dim fSecurity As New DirectorySecurity(folderName, AccessControlSections.All)
        ' Add the FileSystemAccessRule to the security settings. 
        Dim accessRule As FileSystemAccessRule = _
          New FileSystemAccessRule(account, rights, controlType)
    
        fSecurity.AddAccessRule(accessRule)
    
        ' Set the new access settings.
        Directory.SetAccessControl(folderName, fSecurity)
    
      End Sub
    
    End Module
    

     

     

    Appreciate any assistance you can give me.

     

    Greg

     

    06 Mei 2010 23:01

Jawaban

  • Hi Gregory Loehr,

        Sorry for my first glance.

        Here is the solution, hope it is not too late.

    Dim dirName As [String] = "E:\test"
    Dim dSecurity As DirectorySecurity = Directory.GetAccessControl(dirName)
    Dim ac_rule As New FileSystemAccessRule("Users", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)
    dSecurity.AddAccessRule(ac_rule)
    Directory.SetAccessControl(dirName, dSecurity)

     

       Here are some additional reference:

       1. InheritanceFlags Enumeration

       2. PropagationFlags Enumeration


    Please mark the right answer at right time.
    Thanks,
    Sam
    09 Mei 2010 15:20

Semua Balasan

  • Hi,

       Thanks for your posting. Please consider here for better and quicker response.


    Please mark the right answer at right time.
    Thanks,
    Sam
    07 Mei 2010 2:59
  • The link you gave me is for Active Directory programming.

     

    Did you read my question?  What does my question have to do with Active Directory?

    Why did you move my post?

     

    07 Mei 2010 16:43
  • Hi Gregory Loehr,

        Sorry for my first glance.

        Here is the solution, hope it is not too late.

    Dim dirName As [String] = "E:\test"
    Dim dSecurity As DirectorySecurity = Directory.GetAccessControl(dirName)
    Dim ac_rule As New FileSystemAccessRule("Users", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)
    dSecurity.AddAccessRule(ac_rule)
    Directory.SetAccessControl(dirName, dSecurity)

     

       Here are some additional reference:

       1. InheritanceFlags Enumeration

       2. PropagationFlags Enumeration


    Please mark the right answer at right time.
    Thanks,
    Sam
    09 Mei 2010 15:20
  • Hi Gregory,

        Is my reply helpful? If you have any further concerns, please let me know.


    Please mark the right answer at right time.
    Thanks,
    Sam
    10 Mei 2010 12:05
  • Hello !

    I had the same problem. Your solution was helpful. Thank You.

    Roland

    31 Maret 2012 14:49
  • Hi Visual I Sahuji, I Am Very Sorry But This Forum Isn't For VB6 Question But Only Visual Basic .Net Thanks, Omar Emad Eldin
    artikel verzeichnis
    01 April 2012 2:51
  • Hi Visual I Sahuji, I Am Very Sorry But This Forum Isn't For VB6 Question But Only Visual Basic .Net Thanks, Omar Emad Eldin
    artikel verzeichnis
    I cannot see where this is related to VB6 and all references in this thread refer to VB.NET so I am moving it the the VB.NET forum. 

    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer

    Jeff Wharton
    MSysDev (C.Sturt), MDbDsgnMgt (C.Sturt), MCT, MCPD, MCSD, MCITP, MCDBA
    Blog: Mr. Wharty's Ramblings
    Twitter: @Mr_Wharty
    MC ID: Microsoft Transcript

    01 Juni 2012 1:38