473,728 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't share folder to individual user account with WMI.

Hi, All,
I wrote a function to share a folder to individual user account with
When I call it :
ShareFolder("c: \\test", "Test", "Shared by Riven", 8,
Environment.Mac hineName, "Administrator" );
The folder will be shared, but nobody has permission to access it.
(The share permissions list of this folder is empty)
Could you help me?


private void ShareFolder(str ing path, string name, string description,
int maxAllowed, string domain, string account)
//==0. Create Win32_Trustee
ManagementObjec t trustee = new ManagementClass (new
ManagementPath( "Win32_Trustee" ), null);
trustee["Domain"] = null;
trustee["Name"] = account;
string strSid = this.GetSID(dom ain, account);

//==1. Create win32_ace
ManagementObjec t ACE = new ManagementClass (new
ManagementPath( "Win32_Ace" ), null);
ACE["AccessMask "] = AccessMasks.Mod ify;
ACE["AceFlags"] = AceFlags.OBJECT _INHERIT_ACE |
ACE["AceType"] = AceType.Allow;
ACE["Trustee"] = trustee;
//==2. Create Win32_SecurityD escriptor
ManagementObjec t secDescriptor = new ManagementClass (new
ManagementPath( "Win32_Security Descriptor"), null);
secDescriptor["ControlFla gs"] = 4; //SE_DACL_PRESENT
secDescriptor["DACL"] = new object[] { ACE };

//==3. Using Win32_Share
ManagementClass classObj = new ManagementClass (new
ManagementPath( "Win32_Shar e"), null);
ManagementBaseO bject inParams =
classObj.GetMet hodParameters(" Create");
inParams["Access"] = secDescriptor;
inParams["Descriptio n"] = description;
inParams["MaximumAllowed "] = maxAllowed;
inParams["Name"] = name;
//inParams["Password"] = ""; //default is no password
inParams["Path"] = path;
inParams["Type"] = 0; //0: Disk Drive, 1: Print
Queue ,2: Device , 3: IPC

ManagementBaseO bject outParams =
classObj.Invoke Method("Create" , inParams, null);
uint ret =
(uint)(outParam s.Properties["ReturnValu e"].Value);
public string GetSID(string domain, string username)
string result = null;
ManagementScope scope = new ManagementScope ("root\\cimv2") ;
string queryString = string.Format(" SELECT SID FROM
Win32_UserAccou nt WHERE Domain=\"{0}\" AND Name=\"{1}\"", domain,
SelectQuery userQuery = new SelectQuery(que ryString);
ManagementObjec tSearcher userSearcher = new
ManagementObjec tSearcher(scope , userQuery);
ManagementObjec tCollection queryCollection =
userSearcher.Ge t();

foreach (ManagementObje ct userAccount in queryCollection )
result = (string)userAcc ount["SID"];
if (result == null)
throw new Exception("Can' t fine user info!");
return result;

public enum AccessMasks : int
ListDirectory = 0x1,
AddFile = 0x2,
AddSubdirectory = 0x4,
ReadExtendedAtt ributes = 0x8,
WriteExtendedAt tributes = 0x10,
Traverse = 0x20,
DeleteChild = 0x40,
ReadAttributes = 0x80,
WriteAttributes = 0x100,
Delete = 0x10000,
ReadControl = 0x20000,
WriteDac = 0x40000,
WriteOwner = 0x80000,
Synchronize = 0x100000,

Read = ListDirectory | ReadExtendedAtt ributes | ReadAttributes
| ReadControl | Synchronize,
ReadAndExecute = Read | Traverse,
Write = AddFile | AddSubdirectory | WriteExtendedAt tributes |
WriteAttributes | Synchronize,
Modify = Read | Write | Delete | Traverse,
FullControl = DeleteChild | WriteDac | WriteOwner | Modify

public enum AceType : byte
Allow = 0,// Access Allowed
Denied = 1,// Access Denied
Audit = 2
public enum AceFlags : byte


Dec 27 '05 #1
0 2587

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

Similar topics

by: Dave | last post by:
I have an application where I am providing the user the ability to select or create a folder on a domain, using SHBrowseForFolder. When the user selects/creates a folder on a remote computer, it returns the UNC path of that folder. Using ADSI IADsFileShare, I would like to create that share, but the ..Path property seems to reject the UNC. Is there a way of converting that UNC to the logical drive path or some other way of setting the...
by: CES | last post by:
All, If anyone has been following my trials over the last week see(Setting up a Web Application on IIS 5.1 and ASP.Net Security Problems). I'm having a problem running a Asp.Net Web Application. I've finally narrowed the problem down to a security issue: The ASPNET (aspnet_wp) account cannot connect to a Network resource instead of using a local path ie: C:\Inetpub\wwwroot my development site resides on a Network Share...
by: Lucas | last post by:
Hi, I have an ASP.Net application written with VS.Net 2002 (Net FWK 1.0). This Web Application uses Exception Management Application Block to log Events to Windows Event Log. We registered the Exception Management Application Block using installutil. Our Application uses impersonation and used to work fine on Windows 2000 Server. Now we installed it on a Windows Server 2003 and when an exception is raised, we get an "Access is denied"...
by: JR | last post by:
I have a Web server running on Windows XP. On this Web server, I have a Web site configured with its home directory on a network share. In the Web site, there's a virtual folder pointing to a local folder on the Web server containing an ASP.NET application. I get a "Failed to start monitoring changes" when I try to run the application. If I try to debug, I get a message "unable to start debugging on the Web server". I put an...
by: ASP.NET User | last post by:
Hi I am in a shop where developers are required to work off of a networ share. This is so that code and other documentation is backed up nightly. This is outside the realm of Visual SourceSafe which we also use for code control. The network drive is used as the working folder. This is a configuration this shop has used for 6 years and I do not see it changing any time soon So I am testing running an ASP.NET Web application with a Clas...
by: pj_servadmin | last post by:
Server Error in '/<applicationName>' Application -------------------------------------------------------------------------------- Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. Parser Error Message: Could not load type 'Default.Global'. Source Error: Line 1: <%@ Application...
by: John Fleming | last post by:
Hello, I am building a web application with the following components: 2 Web Servers - Windows Server 2003 - IIS 6 - .Net Framework version 2.0 - Reside in Domain A 2 Clustered Database Servers
by: shapper | last post by:
Hello, In have a class inside a compiled DLL where I have the following code: ' Create site map XML document Dim siteMap As XmlDocument = New XmlDocument ' Load site map XML document from ASP.NET web site map siteMap.Load(HttpContext.Current.Server.MapPath(Me.SiteMapUrl)) Me.SiteMapUrl is a property.
by: amitcool1982 | last post by:
I am getting the path of share folder "ADMIN$" on a remote machine but this command have any user account issue. if it have any issues then what is the issue. If any developer have any clue then reply me.
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.