[2005] grant folder access to all users-VBForums
Results 1 to 6 of 6

Thread: [2005] grant folder access to all users

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    251

    [2005] grant folder access to all users

    In my application, I create data and log folders that must be shared by all users using this computer. I did a lot of research, and I found a function that is supposed to grant full access to every users of a folder and its children. However if right-click properties on my folder, the access is unchanged.

    How should I grant full access to all users of a folder and its children (I don't care who created it, all users should be able to read and write to that folder)?

    Code:
        Public Sub GrantAllAccess(ByVal Folder As String)
    
            Try
                Dim DomainUser As String = Environment.UserDomainName & "\" & Environment.UserName
                Dim Dinfo As New DirectoryInfo(Folder)
                Dim Dsecurity As DirectorySecurity = Dinfo.GetAccessControl
                Dsecurity.AddAccessRule(New FileSystemAccessRule(DomainUser, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow))
                Dsecurity.AddAccessRule(New FileSystemAccessRule(DomainUser, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))
    
            Catch ex As Exception
                debug.print("GrantAllAccess EXCEPTION:")
                debug.print(ex.ToString)
            End Try
    
        End Sub

  2. #2
    Moderator.NET kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,125

    Re: [2005] grant folder access to all users

    In your research, did you come across the folder

    My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData

    which is the correct place to store application data that needs read and write access and needs to be accessible to all users of a computer?

    It also maps correctly regardless of OS version.
    Using VB.NET 2010/.NET 2.0 through 4.0 * Please mark you thread resolved using the Thread Tools above
    PLEASE INDICATE WHAT VERSION OF VB YOU USE!!!!!!!!!!!
    * If you found a post useful then please Rate it! * DO NOT PM ME WITH LINKS TO YOUR THREADS FOR ANSWERS PLEASE!

    Code Bank:Manipulate HTML Page content in the Web Browser Control from VB - Drag Drop from Windows into Win Form - Launch new default browser instance to open URL - Display Internet Image in Picturebox - Download Files From Web With Progress Bar - IP Textbox User Control - Installing .NET Framework with INNO Setup
    ZerosAndTheOne.com
    -=Matt=-

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    251

    Re: [2005] grant folder access to all users

    This is exactly what I am currently doing to store my Logs (on XP it maps to "C:\Documents and Settings\All users\Application Data\Company\Program\Version") but if the folder is created with user A, then I switch to user B, I get a permission error on that folder.

    However for my data I cannot use the standard folder because it needs over 4 Gb of free space which may not always be available on C, so I let the user choose the drive and folder of his choice. However that particular folder must also be shared by all users of this computer.

    There must be a way with Windows to create a "public" folder for everybody without having to manually set permissions on that folder?

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [2005] grant folder access to all users

    Quote Originally Posted by JacquesLebrun
    There must be a way with Windows to create a "public" folder for everybody without having to manually set permissions on that folder?
    How can that possibly make sense :S
    Your basically saying there must be a way to set the permissions of a folder to allow everyone to access it... without setting the permissions of a folder.

    Anyway, your users should not be having permissions problems accessing directories within the AllUsers directory... How are you creating the directories in there?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Moderator.NET kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,125

    Re: [2005] grant folder access to all users

    I think by "manually" he means he wants it done in code versus having the user need to do it.

    I do agree about the all users thing. I want to go and test that later on one of my VMs to see what the deal is with that.
    Using VB.NET 2010/.NET 2.0 through 4.0 * Please mark you thread resolved using the Thread Tools above
    PLEASE INDICATE WHAT VERSION OF VB YOU USE!!!!!!!!!!!
    * If you found a post useful then please Rate it! * DO NOT PM ME WITH LINKS TO YOUR THREADS FOR ANSWERS PLEASE!

    Code Bank:Manipulate HTML Page content in the Web Browser Control from VB - Drag Drop from Windows into Win Form - Launch new default browser instance to open URL - Display Internet Image in Picturebox - Download Files From Web With Progress Bar - IP Textbox User Control - Installing .NET Framework with INNO Setup
    ZerosAndTheOne.com
    -=Matt=-

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    251

    Re: [2005] grant folder access to all users

    I never give up!

    I found a very good document that explains how Windows security can be set programmatically with the Framework 2.0 and above:

    http://msdn.microsoft.com/fr-ca/maga...85(en-us).aspx

    Here is a function that successfully grants FullControl to Everyone on a folder and its childs. I use it to store shared data which is downloaded from internet by any users who use my program. You can also change the Grant parameters for more restrictive permissions.

    Code:
    Imports System.IO
    Imports System.Security.AccessControl
    Imports System.Security.Principal
    
    Public Class Form1
    
        '-----------------------------------------------------------------------------
        ' This function grants FullControl to Everyone on a folder and its descendants
        ' The user that runs this program must have the necessary priviledges
        '-----------------------------------------------------------------------------
        Private Sub GrantFullControlToEveryone(ByVal Folder As String)
    
            ' Get security settings of that folder
            Dim Security As DirectorySecurity = Directory.GetAccessControl(Folder)
    
            ' Get language independant security identifier for "Everyone"
            Dim Sid As New SecurityIdentifier(WellKnownSidType.WorldSid, Nothing)
    
            ' Create an account with the Everyone user
            Dim Account As NTAccount = TryCast(Sid.Translate(GetType(NTAccount)), NTAccount)
    
            ' Create FullControl permission for folder and its children
            Dim Grant As New FileSystemAccessRule(Account, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)
    
            ' Assign permission to security object
            Security.AddAccessRule(Grant)
    
            ' Grant R/W permissions to Everyone for this folder
            Directory.SetAccessControl(Folder, Security)
    
        End Sub
    
    End Class

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.