Use WMI to list user rights for a directory in Windows (C #)

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 ();

}

}

//




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.