gpt4 book ai didi

c# - C# 中的读取共享权限

转载 作者:行者123 更新时间:2023-11-30 13:28:19 25 4
gpt4 key购买 nike

是否可以读取分配给共享文件夹的共享权限?我能够以编程方式读取本地安全设置(在右键单击 > 属性 > 安全下找到的设置)没问题。但是,我想知道如何读取右键单击 > 共享和安全... > 权限下的权限

这是我想阅读的权限的图像:

Share Permissions

这可能吗?如果有帮助,我正在运行 XP Pro 机器。

编辑:

根据我的回答,我能够遍历所有共享,并获得(即运行该程序的人)对该共享的访问权限,但还没有找到阅读的方法其他人对该共享的权限。这是使用 Win32_Share 完成的类,但是它没有获取其他用户共享权限的选项。如果有人有任何有用的提示,那将是一个巨大的帮助。

最佳答案

我能够通过扩展 the approach taken by Petey B. 来实现这个功能此外,请确保运行此代码的进程模拟服务器上的特权用户。

    using System;
using System.Management;

...

private static void ShareSecurity(string ServerName)
{
ConnectionOptions myConnectionOptions = new ConnectionOptions();

myConnectionOptions.Impersonation = ImpersonationLevel.Impersonate;
myConnectionOptions.Authentication = AuthenticationLevel.Packet;

ManagementScope myManagementScope =
new ManagementScope(@"\\" + ServerName + @"\root\cimv2", myConnectionOptions);

myManagementScope.Connect();

if (!myManagementScope.IsConnected)
Console.WriteLine("could not connect");
else
{
ManagementObjectSearcher myObjectSearcher =
new ManagementObjectSearcher(myManagementScope.Path.ToString(), "SELECT * FROM Win32_LogicalShareSecuritySetting");

foreach(ManagementObject share in myObjectSearcher.Get())
{
Console.WriteLine(share["Name"] as string);
InvokeMethodOptions options = new InvokeMethodOptions();
ManagementBaseObject outParamsMthd = share.InvokeMethod("GetSecurityDescriptor", null, options);
ManagementBaseObject descriptor = outParamsMthd["Descriptor"] as ManagementBaseObject;
ManagementBaseObject[] dacl = descriptor["DACL"] as ManagementBaseObject[];

foreach (ManagementBaseObject ace in dacl)
{
try
{
ManagementBaseObject trustee = ace["Trustee"] as ManagementBaseObject;
Console.WriteLine(
trustee["Domain"] as string + @"\" + trustee["Name"] as string + ": " +
ace["AccessMask"] as string + " " + ace["AceType"] as string
);
}
catch (Exception error)
{
Console.WriteLine("Error: "+ error.ToString());
}
}
}
}
}

关于c# - C# 中的读取共享权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6227892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com