Solved

Set Write Permissions for a Group - VB.NET

Posted on 2009-07-02
2
335 Views
Last Modified: 2013-11-26
Hi Experts Exchange!

I'm trying to do something that seems pretty simple to me, however, after a couple hours of research I can't seem to find exactly what I need.

I'm creating an xml file, to store some settings, in the [My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData] directory of windows.  The file is created without any problems, however, the "Users" group is only given Read & Excecute permissions to the file.  So, when another user runs my program and attempts to modify the the .xml file, a security permissions error is thrown.  

Here's what I need to do...
I need to change the file's permissions for the "Users" group, and allow write access to the .xml file.  I'd also like to do this immediately after the file's creation.

Just an FYI; this in no way creates any security threat for my application.  Not only is my .xml file encrypted, but the settings stored inside the .xml file are of an inconsequential nature.  It's encrypted simply for the peace of mind associated with basic security through obfuscation.

That's pretty straight forward right?  I'm sure plenty of you guys have changed windows security permissions at the group level before.

A note...I did try assigning the attribute <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> to my methods, however this was not successful.

If you have any questions, please feel free to ask away!

Much thanks!   =)
0
Comment
Question by:OFGemini
2 Comments
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 24807667
Here is an example of doing precisely that...
You'd use it like this:
Imports System.Security.AccessControl
            Dim domainSid As New Security.Principal.SecurityIdentifier("S-1-0-0")
            Dim Users As New Security.Principal.SecurityIdentifier(Security.Principal.WellKnownSidType.BuiltinUsersSid, domainSid)
            AddFileSecurity(wp_file, Users, FileSystemRights.Modify, AccessControlType.Allow)

Imports System.Security.AccessControl
Imports System.Security.Principal
 
    '
    ' Adds an ACL entry on the specified file for the specified account.
    '
    Public Sub AddFileSecurity(ByVal fileName As String, ByVal account As SecurityIdentifier, 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)
 
        ' Create a new rule
        Dim accessRule As New FileSystemAccessRule(account, rights, controlType)
 
        ' Add the FileSystemAccessRule to the security settings. 
        fSecurity.AddAccessRule(accessRule)
 
        ' Set the new access settings.
        File.SetAccessControl(fileName, fSecurity)
    End Sub

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now