473,322 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Add and Remove Folder Permissions in VB.Net

Hi

I'm using WMI to set and remove folderpermissions and it sems to work
fine, sometimes. I start by having the folderpermissons manuly set to
Everyone and Everone has full rights. When I'm setting and removing
permissons on a mapped folder in the network it works fine but when
doing the same thing on a folder om my harddrive Everyone does not get
removed.
Does anyone have any idea how to fix this or how to set and remove
folderpermissons in another way.
Here is the code I'm using:
Try
'Path to folder to change permissions for

Dim mp As New ManagementPath()

'mp.Server = "servername"

'mp.NamespacePath = "root\cimv2"

mp.RelativePath =
"\\MASCHINENAME\root\CIMV2:Win32_LogicalFileSecuri tySetting.Path=""PATHTOFOLDER"""
'"Win32_LogicalFileSecuritySetting.Path='" +
path.Replace("\", "\\") + "'"
Dim objFile As New ManagementObject(mp)

Dim options As New InvokeMethodOptions(Nothing, New
TimeSpan(0, 0, 0, 5))

Dim outparams As ManagementBaseObject =
objFile.InvokeMethod("GetSecurityDescriptor", Nothing, options)

Dim securityDescriptor As ManagementBaseObject =
outparams("Descriptor")

lblMessage.Text += "Got SD...<br>"

Dim dacl As ManagementBaseObject() =
securityDescriptor("DACL")

Dim oldACE As ManagementBaseObject

Dim trustee As ManagementBaseObject

lblMessage.Text += "Print old DACL<br>"

For Each oldACE In dacl

trustee = CType(oldACE("Trustee"),
ManagementBaseObject)

lblMessage.Text += trustee("Name").ToString() & " " &
oldACE("AccessMask").ToString() & " " & oldACE("AceType").ToString() &
"<br>"

Next

Dim win32Trustee As New ManagementClass("Win32_Trustee")

'Create Trustee for User

Dim newTrusteeUser As ManagementObject =
win32Trustee.CreateInstance

Dim UserAcct As String = "LDAP string to user in
AD (without LDAP://)"

Dim UserNamePath As String = "LDAP://" & UserAcct

Dim dirEnt As New DirectoryEntry(UserNamePath)

Dim UserName As String =
dirEnt.Properties("sAMAccountName")(0)

Dim UserSid As Byte() = dirEnt.Properties("objectsid")(0)

dirEnt.Dispose()

newTrusteeUser("Name") = UserName

newTrusteeUser("SID") = UserSid

newTrusteeUser("SIDLength") = UserSid.Length

'Create ACE for User

Dim win32Ace As New ManagementClass("Win32_ACE")

Dim newACEUser As ManagementObject =
win32Ace.CreateInstance

newACEUser("Trustee") = newTrusteeUser

newACEUser("AceFlags") = 3

newACEUser("AceType") = 0

newACEUser("AccessMask") = 1179817

'Create Trustee for Domain Admin

Dim newTrusteeAdmin As ManagementObject =
win32Trustee.CreateInstance

Dim AdminAcct As String ="LDAP string to Domain Admins in
AD (without LDAP://)"

Dim AdminNamePath As String = "LDAP://" & AdminAcct

dirEnt = New DirectoryEntry(AdminNamePath)

Dim AdminName As String =
dirEnt.Properties("sAMAccountName")(0)

Dim adminSid As Byte() = dirEnt.Properties("objectsid")(0)

dirEnt.Dispose()

newTrusteeAdmin("Name") = AdminName

newTrusteeAdmin("SID") = adminSid

newTrusteeAdmin("SIDLength") = adminSid.Length

'Create ACE for Domain Admins

Dim newACEAdmin As ManagementObject =
win32Ace.CreateInstance

newACEAdmin("Trustee") = newTrusteeAdmin

newACEAdmin("AceFlags") = 3

newACEAdmin("AceType") = 0

newACEAdmin("AccessMask") = 2032127

'set new dacl

Dim newAces() As ManagementBaseObject = New
ManagementBaseObject() {newACEUser, newACEAdmin}

securityDescriptor("DACL") = newAces

'call method, set sd

Dim args1() As Object = {securityDescriptor}

Dim retval As UInt32 =
objFile.InvokeMethod("SetSecurityDescriptor", args1)

lblMessage.Text += "<br>SetSecurityDescriptor ReturnStatus
= " & System.Convert.ToInt32(retval)
Catch ex As Exception

lblMessage.Text = "Setting permission failed: " &
ex.Message

End Try

End Sub
Nov 20 '05 #1
0 3596

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrew Crowe | last post by:
Hi guys, We're using the ISAPI version of PHP with IIS5.0 On some websites for some reason any php file in the websites root is given the path winnt/system32 (getcwd() returns...
7
by: Kim Lots | last post by:
Hi Sorry to disturb you again but i really like to know what's the NTFS folder permissions on a "virtual directory" folder for a public webserver iis 5.x running ASP 3.0 with an Access DB on a...
3
by: roger | last post by:
I've created a web setup project for my aspnet app, and added a web folder item "tmp" to the File System editor under the web application folder. (My app uses this folder for generating image...
1
by: Codex Twin | last post by:
Hello folks Is it possible to add/remove Read Permissions (and any other NTFS permissions) to a folder programmatically via an ASP.NET page? Thanks.
0
by: Johan | last post by:
Hi I'm using WMI to set and remove folderpermissions and it sems to work fine, sometimes. I start by having the folderpermissons manuly set to Everyone and Everone has full rights. When I'm...
6
by: Jeff | last post by:
Hi - I understand how to create a directory folder, but how can I programatically create a _shared_ directory folder and set its permissions?? (I'm using VB.NET.) Thanks for your help. -...
5
by: Mitchell S. Honnert | last post by:
Is there a way, given the full path of a folder on a network, that one can programatically tell if you have Read access to that folder? I have an application where the user is able to select a...
2
by: skinnybloke | last post by:
Hi - I have a file permissions problem with PHP. I am using a product called Actinic to develop a shopping cart. This creates a subfolder named ACATALOG into which it loads all of its files. The...
6
by: Andy Fish | last post by:
Hi, From what I can gather, under Asp.Net 2.0, it is safe to put data and config files underneath the app_data in the web root and they will not be served directly to the browser from a URL. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.