Adding Security to a Folder

I've got functionality in an application that allows me to log on as a specific user; that allows me to test the security for that user or a class of users. 

The problem is that part of the functionality for the app is to write config files to the user's profile that saves form/grid state. When I do this with my admin account, I end up writing a config file that has my security; and then when the user logs on that can't access the config file and that generates a permissions error (can't open file)

So... Below is code to add myself AND the user to the security profile for a folder so that we're both covered. No more perm errors. 

 

Dim directoryProperties As System.IO.DirectoryInfo = New DirectoryInfo(App.ApplicationDataDirectory)
 
'Get a DirectorySecurity object that represents the current security settings.
Dim dSecurity As System.Security.AccessControl.DirectorySecurity = directoryProperties.GetAccessControl()
 
'Set the new access settings for my user
dSecurity.AddAccessRule(New FileSystemAccessRule("sgray@mydomain.com", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow))
dSecurity.AddAccessRule(New FileSystemAccessRule("sgray@mydomain.com", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))
 
'add security for users other than me
Dim strUser As String = String.Format("{0}@mydomain.com", App.UserName)
Select Case App.UserName.ToUpper
    Case "SGRAY"
    Case Else
        dSecurity.AddAccessRule(New FileSystemAccessRule(strUser, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow))
        dSecurity.AddAccessRule(New FileSystemAccessRule(strUser, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))
End Select
 
 
directoryProperties.SetAccessControl(dSecurity)

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences