.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
使用WMI列出Windows中某个目录的用户权限(C#)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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

     }

}

//




相关文章

相关软件