本文介绍了如何获取(添加用户或删除用户)共享文件夹的权限列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取共享文件夹的所有访问权限(注意:请注意,这不是安全权限)
但是我能够获得该文件夹的所有安全规则。
我尝试过:
我试过以下代码:
I am trying to get all the access permissions of a shared folder ( note : please note that this is not security permissions )
But i can able to get all the security rules of the folder.
What I have tried:
I have tried below codes:
var directoryInfo = new DirectoryInfo(sharedFolderPath);
var directorySecurity = directoryInfo.GetAccessControl();
var currentUserIdentity = WindowsIdentity.GetCurrent();
foreach (FileSystemAccessRule rule in directoryInfo.GetAccessControl().GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier)))
{
}
当我试图查看这个foreach所有项目时。我得到所有安全规则而不是共享文件夹权限列表
任何人都可以帮我这个
When i try to view this foreach all items . I get all the security rules rather than share folder permissions list
Can anyone help me on this
推荐答案
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());
}
}
}
}
}
这篇关于如何获取(添加用户或删除用户)共享文件夹的权限列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!