Use WMI to list user rights for a directory in Windows (C #)
Last Update:2017-02-28
Source: Internet
Author: User
window| user permissions using System;
Using System.Management;
Using System.Collections;
Class Tester
{
public static void Main ()
{
Try
{
Managementpath path = new Managementpath ();
Path. Server = ".";
Path. NamespacePath = @ "root\cimv2";
Path. RelativePath = @ "win32_logicalfilesecuritysetting.path= ' c:\\test '"; Using TMP as folder name
ManagementObject LFS = new ManagementObject (path);
Dump All trustees (this includes owner)
foreach (Managementbaseobject b in LFS. Getrelated ())
Console.WriteLine ("Trustee: {0} \ t SID [{1}]", b["AccountName"], b["Sid"]);
Get the security descriptor for this object
Managementbaseobject outparams = LFS. InvokeMethod ("Getsecuritydescriptor", NULL, NULL);
if ((UINT) (outparams.properties["ReturnValue"). Value) = = 0)
{
Managementbaseobject descriptor = (managementbaseobject) (outparams.properties["descriptor"). Value));
Managementbaseobject[] Daclobject = (managementbaseobject[]) (descriptor.properties["Dacl"). Value));
Dumpaces (Daclobject);
Managementbaseobject Ownerobject = (managementbaseobject) (descriptor.properties["Owner"). Value));
Dumpownerproperties (ownerobject.properties); Show owner Properies
}
}
catch (Exception e)
{
Console.WriteLine (e);
Console.ReadLine ();
}
}
static void Dumpaces (managementbaseobject[] daclobject)
{
ACE Masks See:winnt.h
String[] Filedesc = {"File_read_data", "File_write_data", "File_append_data", "File_read_ea",
"File_write_ea", "File_execute", "File_delete_child", "File_read_attributes",
"File_write_attributes", "", "" "," ",
" ", " ", " ", " ",
"DELETE", "Read_control", "Write_dac", "Write_owner",
"SYNCHRONIZE", "", "" "," ",
"Access_system_security", "maximum_allowed", "" "," ",
"Generic_all", "Generic_execute", "Generic_write", "Generic_read"};
foreach (Managementbaseobject mbo in Daclobject)
{
Console.WriteLine ("-------------------------------------------------");
Console.WriteLine ("Mask: {0:x}-AceFlags: {1}-AceType: {2}", mbo["AccessMask"], mbo["AceFlags"], mbo["AceType"]);
Access allowed/denied ACE
if (mbo["AceType"]. ToString () = = "1")
Console.WriteLine ("DENIED ACE TYPE");
Else
Console.WriteLine ("Allowed ACE TYPE");
Dump Trustees
Managementbaseobject trustee = (managementbaseobject) (mbo["trustee"));
Console.WriteLine ("Name: {0}-Domain: {1}-SID {2}\n"),
trustee.properties["Name"]. Value,
trustee.properties["Domain". Value,
trustee.properties["SidString"]. Value);
Dump ACE mask in readable form
UInt32 mask = (UInt32) mbo["AccessMask"];
Int[] m = {(int) mask};
BitArray ba = new BitArray (m);
int i = 0;
IEnumerator baenum = ba. GetEnumerator ();
while (Baenum.movenext ())
{
if ((bool) baenum.current)
Console.WriteLine ("\t[{0}]", Filedesc[i]);
i++;
}
}
}
static void Dumpownerproperties (Propertydatacollection Owner)
{
Console.WriteLine ("=============== Owner Properties ========================");
Console.WriteLine ();
Console.WriteLine ("Domain {0} \tname {1}", owner["domain"). Value, owner["Name". Value);
Console.WriteLine ("SID \t{0}", owner["sidstring"]. Value);
Console.ReadLine ();
}
}
//