C#-如何获取远程计算机上有权访问共享文件夹的用户/组的列表

本文关键字:文件夹 访问共享 用户 列表 计算机 何获取 获取 C#- | 更新日期: 2023-09-27 18:21:28

我想用C#编写一段代码,列出所有可以访问(读/写/满)服务器上共享文件夹的用户/组。

例如:我有一个共享文件夹''servername''MyData。现在我想列出有权访问此文件夹的用户/组。

C#-如何获取远程计算机上有权访问共享文件夹的用户/组的列表

这应该会让你指向正确的方向,我不能在atm上测试它,但应该是类似的。

private bool CheckAccess(DirectoryInfo directory)
{
    // Get the collection of authorization rules that apply to the current directory
    AuthorizationRuleCollection acl = directory.GetAccessControl().GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
    foreach (var rule in acl)
    {
        // do something here
    }
}
    private DataTable GetSharedFolderAccessRule()
    {
        DataTable DT = new DataTable();
        try
        {
            DT.Columns.Add("ShareName");
            DT.Columns.Add("Caption");
            DT.Columns.Add("Path");
            DT.Columns.Add("Domain");
            DT.Columns.Add("User");
            DT.Columns.Add("AccessMask");
            DT.Columns.Add("AceType");
            ManagementScope Scope = new ManagementScope(@"''.'root'cimv2");
            Scope.Connect();
            ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_LogicalShareSecuritySetting");
            ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
            ManagementObjectCollection QueryCollection = Searcher.Get();
            foreach (ManagementObject SharedFolder in QueryCollection)
            {
                {
                    String ShareName = (String) SharedFolder["Name"];
                    String Caption   = (String)SharedFolder["Caption"];
                    String LocalPath = String.Empty;
                    ManagementObjectSearcher Win32Share = new ManagementObjectSearcher("SELECT Path FROM Win32_share WHERE Name = '" + ShareName + "'");
                    foreach (ManagementObject ShareData in Win32Share.Get())
                    {
                        LocalPath = (String) ShareData["Path"];
                    }
                    ManagementBaseObject Method = SharedFolder.InvokeMethod("GetSecurityDescriptor", null, new InvokeMethodOptions());
                    ManagementBaseObject Descriptor = (ManagementBaseObject)Method["Descriptor"];
                    ManagementBaseObject[] DACL = (ManagementBaseObject[])Descriptor["DACL"];
                    foreach (ManagementBaseObject ACE in DACL)
                    {
                        ManagementBaseObject Trustee = (ManagementBaseObject)ACE["Trustee"];
                        DataRow Row = DT.NewRow();
                        Row["ShareName"]  = ShareName;
                        Row["Caption"]    = Caption;
                        Row["Path"]       = LocalPath;
                        Row["Domain"]     = (String) Trustee["Domain"];
                        Row["User"]       = (String) Trustee["Name"];
                        Row["AccessMask"] = (UInt32) ACE["AccessMask"];
                        Row["AceType"]    = (UInt32)ACE["AceType"];
                        DT.Rows.Add(Row);
                        DT.AcceptChanges();
                    }
                }
            }
        }
        catch (Exception ex) 
        {
            MessageBox.Show(ex.StackTrace, ex.Message);
        }
        return DT;
    }