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

发布于 2024-12-28 18:48:36 字数 113 浏览 2 评论 0原文

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

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

I wanted to write a code in C# to list all the users/groups having access (Read/Write/Full) to a shared folder on a server.

For Example: I have a shared folder \servername\MyData. Now I wanted to list the users/groups who have access to this folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ペ泪落弦音 2025-01-04 18:48:36

这应该会让你指向正确的方向,我无法测试它,但应该是类似的东西。

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
    }
}

This should get you pointed in the right direction, I can't test it atm but should be something similar.

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
    }
}
苯莒 2025-01-04 18:48:36
    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;
    }
    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文