使用WMI列出Windows中某个目录的用户权限(C#)
来源:互联网 发布:航空售票系统java项目 编辑:程序博客网 时间:2024/06/03 13:31
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();
}
}
//
- 使用WMI列出Windows中某个目录的用户权限(C#)
- WMI列出Windows中某个目录的用户权限
- WMI列出Windows中某个目录的用户权限
- .NET_C#_WMI相关_列出Windows中某个目录的用户权限
- C语言列出某个目录下的文件
- 列出某个目录下的所有文件
- 使用WMI来控制Windows目录
- 通过WMI列出Windows系统进程(VB.net)
- 如何列出某个目录下的所有目录和文件
- 列出某个目录下的所有文件和列出某目录下所有的子目录
- Linux 列出某个进程的具体的启动目录
- 列出某个目录下的所有子目录和文件
- 列出某个文件夹下的文件及目录
- [IO]如何列出某个目录下的所有文件
- [IO]如何列出某个目录下的所有子目录
- [Python]GUI--列出某个目录下的文件
- 列出某个目录下的所有Java文件
- 如何列出某个目录下的所有文件?
- Empowering the ASP.Net Button Web Control, Confirm?...
- 通过WMI列出Windows系统进程(VB.net)
- 征人帮我对Unix数据库进行攻击!
- 041006 A DB 乱码
- 網頁中彈出對自定義對話框並進行傳值
- 使用WMI列出Windows中某个目录的用户权限(C#)
- 041006 A WebService的构造函数
- asp.net中的多語言使用
- IP到数字转换
- 伤感总是在那么一瞬间
- 《设计模式Design Pattern》读书笔记之四
- 生日快乐
- Vsiaul C#如何读取注册信息
- 运卦