【问题标题】:Reading Share Permissions in C#在 C# 中读取共享权限
【发布时间】:2011-09-07 20:17:37
【问题描述】:

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

这是我要阅读的权限图片:

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

编辑:

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

【问题讨论】:

    标签: c# permissions share folder-permissions


    【解决方案1】:

    我知道您可以使用 Windows Home Server: http://msdn.microsoft.com/en-us/library/bb425864.aspx

    您可以在 MMC 中执行此操作,其中大部分可通过代码获得,因此应该可以。如果您在那里找不到它,那么您应该检查 Windows API 调用。我已经看到它是用 C++ 完成的,所以它也应该可以在 C# 中实现。抱歉,我没有任何示例代码或其他链接来提供这些。不过我看看能不能挖出来。

    我也刚刚在 SO 上看到了这个: how to create shared folder in C# with read only access?

    另一个很好的链接: http://social.msdn.microsoft.com/Forums/en/windowssdk/thread/de213b61-dc7e-4f33-acdb-893aa96837fa

    【讨论】:

    • 感谢您的信息。运行 Windows XP 时是否可以引用 Microsoft.HomeServer.SDK.Interop.v1?
    • 我不知道答案,但我不这么认为。看看其他链接,它们应该为您提供足够的信息来完成您的任务。
    • 谢谢,我正准备根据我的回答收集我对每个共享的权限
    【解决方案2】:

    我能想到的最好办法是遍历机器上的所有共享并读取对该共享的权限。

    ManagementClass manClass = new ManagementClass(@"\\" +computerName +@"\root\cimv2:Win32_Share"); //get shares
    
    //run through all the shares
    foreach (ManagementObject objShare in manClass.GetInstances())
    {
      //ignore system shares
      if (!objShare.Properties["Name"].Value.ToString().Contains('$'))
      {
        //print out the share name and location
        textBox2.Text += String.Format("Share Name: {0}  Share Location: {1}", objShare.Properties["Name"].Value, objShare.Properties["Path"].Value) + "\n";
    
        Int32 permissions = 0;
    
        try
        {
          //get the access values you have
          ManagementBaseObject result = objShare.InvokeMethod("GetAccessMask", null, null);
    
          //value meanings: http://msdn.microsoft.com/en-us/library/aa390438(v=vs.85).aspx
          permissions = Convert.ToInt32(result.Properties["ReturnValue"].Value);
        }
        catch (ManagementException me)
        {
          permissions = -1; //no permissions are set on the share
        }
    
        textBox2.Text += "You have permissions: " + permissions + "\n\n";
    
      }
    }
    

    如果有人能弄清楚如何获得其他人对共享的权限,那就太棒了。

    【讨论】:

      【解决方案3】:

      我可以通过扩展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());
                          }
                      }
                  }               
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-11
        • 2011-03-12
        • 2010-09-13
        • 2016-01-20
        • 2019-04-30
        • 2021-06-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多