是否可以读取分配给共享文件夹的共享权限?我能够在本地安全设置programmaticaly(在右键单击>属性>安全)下找到没有问题。但是,我想知道如何在Right Click>下阅读权限。分享和安全...>权限
以下是我想要阅读的权限的图片:
这可能吗?如果有帮助,我正在运行XP Pro机器。
编辑:
根据我的回答,我能够遍历所有共享,并获得访问你(即运行程序的人)访问该共享,但还没有找到方法来阅读权限其他对该共享具有权限。这是使用Win32_Share类完成的,但它没有获取其他用户的共享权限的选项。如果有人有任何有用的提示,那将是一个巨大的帮助。
答案 0 :(得分:7)
我能够通过扩展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());
}
}
}
}
}
答案 1 :(得分:2)
我知道你可以使用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
答案 2 :(得分: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";
}
}
如果有人能弄清楚如何获得其他人对共享的权限,这将是惊人的。