.NET_C#_WMI相关_列出Windows中某个目录的用户权限

using System;

using System.Management;

using System.Collections;

 

class Tester

{

 

     public static void Main()

     {

         try

         {

              // ManagementPath类,提供一个包装用于分析以及生成WMI对象的路径

              ManagementPath path = new ManagementPath( );

              // ManagementPath.Server 获取或设置路径的服务器部分

              path.Server = ".";

              // ManagementPath.NamespacePath 获取或设置路径的命名空间部分

              path.NamespacePath = @"root/cimv2";

              // ManagementPath.RelativePath 获取或设置相对路径:只包含类名和键

              path.RelativePath = @"Win32_LogicalFileSecuritySetting.Path='c://test'"; // using tmp as folder name

             

              //ManagementObject 表示数据管理对象

              ManagementObject lfs = new ManagementObject(path);

              // Dump all trustees (this includes owner)

       // MangegementBaseObject 包含管理对象的基本元素,用作更具体的管理对象类的基类

            // ManagementObject.GetRelated()获取与该对象(联系对象)相关的对象的集合

              foreach (ManagementBaseObject b in lfs.GetRelated())

                   Console.WriteLine("Trustee: {0} /t SID [{1}]", b["AccountName"], b["SID"]);

              // Get the security descriptor for this object

              // ManagementObject.InvokeMethod 对对象调用方法

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

     }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值