gpt4 book ai didi

c# - 如何在 .NET Core 3.1.0 中使用 ACL 在 Mac OS 上管理文件夹权限

转载 作者:行者123 更新时间:2023-12-04 01:17:58 24 4
gpt4 key购买 nike

我在 C# 中有以下代码:

if (!Directory.Exists(inputFolder))
{
return true;
}
else
{
var me = new DirectoryInfo(inputFolder);

AuthorizationRuleCollection rules = null;
WindowsIdentity identity = null;
try
{
rules = me.GetAccessControl().GetAccessRules(true, true, typeof(SecurityIdentifier));
identity = WindowsIdentity.GetCurrent();
}
catch (UnauthorizedAccessException uae)
{
return false;
}

string userSID = identity.User.Value;

foreach (FileSystemAccessRule rule in rules)
{
if ((rule.FileSystemRights.HasFlag(FileSystemRights.Read) ||
rule.FileSystemRights.HasFlag(FileSystemRights.ReadAttributes) ||
rule.FileSystemRights.HasFlag(FileSystemRights.ReadData) ||
rule.FileSystemRights.HasFlag(FileSystemRights.ReadAndExecute)) && rule.AccessControlType == AccessControlType.Deny)
{
return false;
}
}

return true;
}

它在 Windows 中正常工作,但是,当我尝试在 mac OS X (Visual Studio for Mac) 上运行它时,它在尝试从方法“GetAccessRules”甚至“GetAccessControl”获取信息时抛出异常说:

"Access Control List (ACL) APIs are part of resource management on Windows and are not supported on this platform."

所以这是我的问题:

在 .net core 中是否有替代方法来执行此操作?或者我应该如何在 .net core 但在 mac OS X 中管理这些权限?

最佳答案

我假设您正在使用 System.IO.FileSystem.AccessControlGetAccessControl().NET Core 上工作?不幸的是,这些 API 与 Windows 低级 API 紧密相关,无法在 Unix/Linux 系统上运行。

但是,还有另一种选择。您可以试试 Mono.Posix.NETStandard。这是 Nuget链接。

您可以调用 UnixDirectoryInfo() 并以这种方式获取或设置 FileAccessPermissions

关于c# - 如何在 .NET Core 3.1.0 中使用 ACL 在 Mac OS 上管理文件夹权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63024370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com