[英]System.MissingMethodException: cannot use dirInfo.GetAccessControl(); in c#
我正在關注microsoft 參考,以便在網絡共享上設置目錄權限。
盡管 Intellicode 沒有顯示任何問題,但只要調用包含dirInfo.GetAccessControl();
的函數,我就會收到以下錯誤dirInfo.GetAccessControl();
System.MissingMethodException: '找不到方法:'System.Security.AccessControl.DirectorySecurity System.IO.DirectoryInfo.GetAccessControl()'。'
要執行的函數如下:
public static void SetFullPermission(string path, string domainAccount)
{
NTAccount ntaccount = new NTAccount("my_domain", domainAccount);
var ds = new DirectorySecurity();
DirectoryInfo dirInfo = new DirectoryInfo(path);
ds = dirInfo.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule(ntaccount, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
ds.SetAccessRuleProtection(true, false); // disable inheritance and clear any inherited permissions
dirInfo.SetAccessControl(ds);
}
一旦我取消注釋var ds = new DirectorySecurity();
代碼被執行但沒有設置權限。
作為參考,以下代碼可能有用。 調用代碼如下所示:
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("my_admin",pwd);
using (new NetworkConnection(@"\\my_fileserver\Data\Home", cred))
{
string path = $@"\\my_fileserver\Data\Home\testuserfolder";
string domainName = "my_domain";
string userNameToCreate = "testuser";
Directory.CreateDirectory(path);
SetFullPermission(path, userNameToCreate);
}
調用SetFullPermission
的代碼利用了如何在連接到網絡共享時提供用戶名和密碼中找到的類,因為我的普通帳戶在網絡驅動器上沒有權限。
NewNetworkConnection()
類如下:
using System;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
namespace FileInterface
{
public class NetworkConnection : IDisposable
{
string _networkName;
public NetworkConnection(string networkName,
NetworkCredential credentials)
{
_networkName = networkName;
var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
var userName = string.IsNullOrEmpty(credentials.Domain)
? credentials.UserName
: string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
var result = WNetAddConnection2(
netResource,
credentials.Password,
userName,
0);
if (result != 0)
{
throw new Win32Exception(result);
}
}
~NetworkConnection()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags,
bool force);
}
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}
public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
}
以下解決方案信息可能很重要:
.NET Core
的主項目Ticketing solution
.NET framework 4.8
的.NET Framework
類庫不同框架目標的原因是一些代碼需要一個框架,其他代碼或參考可能只適用於另一個。
在多目標解決方案中,必須使用 .net 核心代碼,即使目標是 .Net Framework 4.8:
更改GetAccessControl
和SetAccesscontrol
(有關差異,請參閱未注釋的行)
public static void SetFullPermission(string path, string domainAccount)
{
NTAccount ntaccount = new NTAccount("skan_nt1", domainAccount);
var ds = new DirectorySecurity();
DirectoryInfo dirInfo = new DirectoryInfo(path);
ds = FileSystemAclExtensions.GetAccessControl(dirInfo);
//ds = dirInfo.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule(ntaccount, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
ds.SetAccessRuleProtection(true, false); // disable inheritance and clear any inherited permissions
FileSystemAclExtensions.SetAccessControl(dirInfo,ds);
//Directory.SetAccessControl(path, ds);
}
另外,請確保安裝了 nuget 包: System.Security.AccessControl
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.