473,396 Members | 2,024 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.

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
WMI.
When I call it :
ShareFolder("c:\\test", "Test", "Shared by Riven", 8,
Environment.MachineName, "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?

Thanks!

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

//==1. Create win32_ace
ManagementObject ACE = new ManagementClass(new
ManagementPath("Win32_Ace"), null);
ACE["AccessMask"] = AccessMasks.Modify;
ACE["AceFlags"] = AceFlags.OBJECT_INHERIT_ACE |
AceFlags.CONTAINER_INHERIT_ACE;
ACE["AceType"] = AceType.Allow;
ACE["Trustee"] = trustee;
//==2. Create Win32_SecurityDescriptor
ManagementObject secDescriptor = new ManagementClass(new
ManagementPath("Win32_SecurityDescriptor"), null);
secDescriptor["ControlFlags"] = 4; //SE_DACL_PRESENT
secDescriptor["DACL"] = new object[] { ACE };

//==3. Using Win32_Share
ManagementClass classObj = new ManagementClass(new
ManagementPath("Win32_Share"), null);
ManagementBaseObject inParams =
classObj.GetMethodParameters("Create");
inParams["Access"] = secDescriptor;
inParams["Description"] = 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

ManagementBaseObject outParams =
classObj.InvokeMethod("Create", inParams, null);
uint ret =
(uint)(outParams.Properties["ReturnValue"].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_UserAccount WHERE Domain=\"{0}\" AND Name=\"{1}\"", domain,
username);
SelectQuery userQuery = new SelectQuery(queryString);
ManagementObjectSearcher userSearcher = new
ManagementObjectSearcher(scope, userQuery);
ManagementObjectCollection queryCollection =
userSearcher.Get();

foreach (ManagementObject userAccount in queryCollection)
{
result = (string)userAccount["SID"];
}
if (result == null)
{
throw new Exception("Can't fine user info!");
}
return result;
}

[Flags()]
public enum AccessMasks : int
{
ListDirectory = 0x1,
AddFile = 0x2,
AddSubdirectory = 0x4,
ReadExtendedAttributes = 0x8,
WriteExtendedAttributes = 0x10,
Traverse = 0x20,
DeleteChild = 0x40,
ReadAttributes = 0x80,
WriteAttributes = 0x100,
Delete = 0x10000,
ReadControl = 0x20000,
WriteDac = 0x40000,
WriteOwner = 0x80000,
Synchronize = 0x100000,

Read = ListDirectory | ReadExtendedAttributes | ReadAttributes
| ReadControl | Synchronize,
ReadAndExecute = Read | Traverse,
Write = AddFile | AddSubdirectory | WriteExtendedAttributes |
WriteAttributes | Synchronize,
Modify = Read | Write | Delete | Traverse,
FullControl = DeleteChild | WriteDac | WriteOwner | Modify
}

[Flags()]
public enum AceType : byte
{
Allow = 0,// Access Allowed
Denied = 1,// Access Denied
Audit = 2
}
[Flags()]
public enum AceFlags : byte
{
OBJECT_INHERIT_ACE = 0x1,
CONTAINER_INHERIT_ACE = 0x2,
NO_PROPAGATE_INHERIT_ACE = 0x4,
INHERIT_ONLY_ACE = 0x8,
INHERITED_ACE = 0x10

}

Dec 27 '05 #1
0 2576

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

Similar topics

4
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...
1
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...
7
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...
8
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...
11
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...
2
by: pj_servadmin | last post by:
Server Error in '/<applicationName>' Application -------------------------------------------------------------------------------- Parser Error Description: An error occurred during the parsing of...
0
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...
3
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...
0
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...
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
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
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...
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...
0
isladogs
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...

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.