[英]Reading Share Permissions in C#
是否可以讀取分配給共享文件夾的共享權限? 我能夠以編程方式讀取本地安全設置(在右鍵單擊>屬性>安全下找到的設置)沒問題。 但是,我想知道如何在右鍵單擊 > 共享和安全... > 權限下讀取權限
這是我要閱讀的權限的圖像:
這可能嗎? 如果有幫助,我正在運行 XP Pro 機器。
編輯:
根據我的回答,我能夠遍歷所有共享,並獲得您(即運行程序的人)對該共享的訪問權限,但還沒有找到讀取其他人對該共享的權限的方法。 這是使用Win32_Share class 完成的,但是它沒有獲取其他用戶共享權限的選項。 如果有人有任何有用的提示,那將是一個巨大的幫助。
通過擴展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());
}
}
}
}
}
我知道您可以使用 Windows 家庭服務器: http://msdn.microsoft.com/en-us/library/bb425864.aspx
您可以在 MMC 中執行此操作,其中大部分可通過代碼獲得,因此應該是可能的。 如果您在那里找不到它,那么您應該查看 Windows API 調用。 我已經看到它在 C++ 中完成,因此在 C# 中也應該可以。 抱歉,我沒有任何示例代碼或其他鏈接來提供這些。 不過我看看能不能挖出來。
我也剛剛在 SO 上看到了這個: 如何在 C# 中創建具有只讀訪問權限的共享文件夾?
另一個很好的鏈接: http://social.msdn.microsoft.com/Forums/en/windowssdk/thread/de213b61-dc7e-4f33-acdb-893aa96837fa
我能想到的最好的辦法是遍歷機器上的所有共享並讀取您對共享的權限。
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";
}
}
如果有人能弄清楚如何獲得其他人對共享的權限,那就太棒了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.