Results 1 to 5 of 5

Thread: Applying file permissions from .NET code

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Applying file permissions from .NET code

    Hi

    I have an application that is writing a comma separated values file. How would I go about altering the permissions of that file from .NET - for example to give read-only permissions to a particular active directory group?

    Thanks
    Duncan

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Applying file permissions from .NET code

    Are you looking to do this with the readonly attribute, or actually with the ACL?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Applying file permissions from .NET code

    With the ACL.

    What I'm hoping for is:
    Europe\FundAccountants read-only
    Europe\TransferAgency read-write
    Public - no access

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Applying file permissions from .NET code

    Have you seen this link?

    http://dhcollier.com/node/3
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Applying file permissions from .NET code

    I'm not so sure with active directory as I haven't ever had to program against it since my software is generally of the single user commercial client type. However I did have a need to set ACL for a given folder and I used this code. I *think* it could be adapted to work with an AD group, just the same as it works with a built in users group. If not hopefully it will get you in the right direction.

    Code:
    Imports System.Security.AccessControl
    Imports System.IO
    
        Private Function SetUserAccess(ByVal FolderPath As String) As Boolean
    
            Try
    
                Dim myDirectorySecurity As System.Security.AccessControl.DirectorySecurity = System.IO.Directory.GetAccessControl(FolderPath)
    
                Dim UsersGroupIdentifier As New Security.Principal.SecurityIdentifier(Security.Principal.WellKnownSidType.BuiltinUsersSid, Nothing)
    
                Dim myAccessRule As New FileSystemAccessRule(UsersGroupIdentifier, _
                                                             FileSystemRights.FullControl, _
                                                             InheritanceFlags.ObjectInherit Or _
                                                             InheritanceFlags.ContainerInherit, _
                                                             PropagationFlags.InheritOnly, _
                                                             AccessControlType.Allow)
    
                myDirectorySecurity.AddAccessRule(myAccessRule)
    
                Directory.SetAccessControl(FolderPath, myDirectorySecurity)
                Return True
            Catch ex As Exception
                'LOG EXCEPTION HERE
                Return False
            End Try
    
        End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width