473,396 Members | 1,996 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,396 software developers and data experts.

Setting folder permissions using 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=""u:\\jad\\root\\henrik
svensson""" '"Win32_LogicalFileSecuritySetting.Path='" +
path.Replace("\", "\\") + "'"

'\\ASPLUND\root\CIMV2:Win32_LogicalFileSecuritySet ting.Path="d:\\administatorfiles\\root"
'\\ASPLUND\root\CIMV2:Win32_LogicalFileSecuritySet ting.Path="u:\\jad\\root\\henrik
svensson"

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 = "CN=Anna
Hansson,OU=Stab,OU=Lund,DC=qliktech,DC=com" '"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 = "CN=Domain
Admins,CN=Users,DC=qliktech,DC=com" '"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 7898

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

Similar topics

0
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
0
by: Srilatha | last post by:
Hi, I have a folder called "Public_Documents" on my web server. The documents in this folder should be accessible to everyone. I want to create a sub folder here "Private Documents". Only the...
0
by: Praveen | last post by:
Hello. I am writing some code that accepts a DFS Link and Username and grants that User permissions to the physical directory that the DFS Link corresponds to. I am using the System.Management...
2
by: Maximus | last post by:
Hi, Does anybody know how I can programatically set modify permissions on a folder for the aspnet user. Any help will be appreciated. Thanks.
1
by: Jeremy Winchell | last post by:
I have an applications that imports new AD users. It creates a folder to store the user profile information, and a folder for their home directory. I would like to set the Permissions on these...
2
by: Phil Hey | last post by:
I am trying to set permissions on a folder programmatically, and have success fully used the code below for knowledge base article 266461 : How To: Programmatically Set NTFS File System Folder...
2
by: steggun | last post by:
Hello, I'm writing a C# windows form application and need some guidance on a couple of issues. First, my application needs to change the permissions on a folder. The application creates a...
0
by: =?Utf-8?B?TGlhbSBNYWM=?= | last post by:
Hi Folks, I have embeded WMI scripting within a Visual Basic application to create remote shares and set permissions, I'm now moving to vb.net environment and having trouble getting my scripting...
5
by: daokfella | last post by:
I have a custom web.config section similar to the following: <CustomAuthSettings attr1="" attr2=""> <Locations RedirectUrl="Invalid.aspx"> <add Path="test.aspx" Roles="1,2,3" Permissions="4,5,6"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.