المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Problem getting shared folder that created by code using Win32_LogicalShareSecuritySetting



C# Programming
01-23-2010, 01:44 AM
Hi,

How to get a list of all the shared folder and get the share permission for each one?

I tried to use ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_LogicalShareSecuritySetting");

But the line of code above does not return shared folder created by code. If i manually create the folder and set it as Shared folder through windows, the shared folder can be obtain from Win32_LogicalShareSecuritySetting. Just for information, the share and security setting for both folder created through code and manually created is the same. But if the shared folder created using code, it fails to return using Win32_LogicalShareSecuritySetting.

If i use Win32_Share, i'm able to get all the share folders but the problem is i cannot use the method ("GetAccessMask") below to obtain the AccessMask or the share permission information. It will throw exception because GetAccessMask is not a method under Win32_Share:
ManagementBaseObject outParamsMthd =
m.InvokeMethod("GetAccessMask", null, options);

Below is the code snippet to obtain shared folder permission information for user name "Everyone"

ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_LogicalShareSecuritySetting");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);

ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
string shareName = m["Name"].ToString();
string shareLocalPath = ShareManager.GetShareLocalPath(shareName);
Console.WriteLine(shareLocalPath);
if (folderPath == shareLocalPath)
{
InvokeMethodOptions options = new InvokeMethodOptions();

ManagementBaseObject outParamsMthd =
m.InvokeMethod("GetAccessMask", null, options);//GetSecurityDescriptor
ManagementBaseObject descriptor =
outParamsMthd["Descriptor"] as ManagementBaseObject;

ManagementBaseObject[] dacl = descriptor["DACL"] as
ManagementBaseObject[];

foreach (ManagementBaseObject ace in dacl)
{
ManagementBaseObject trustee = ace["Trustee"] as
ManagementBaseObject;
string domain = (string)trustee["Domain"];
if ((string)trustee["Name"] == "Everyone")
{
UInt32 mask = (UInt32)ace["AccessMask"];

// using enum formatting (see emumerating the possibilities.doc)
string enumMask = System.Enum.Format(typeof(Mask), mask, "g");
if (enumMask.Contains("WRITE"))
{
return true;
}
}
}
}

Thanks in advance