[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
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.
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?
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?
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.
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