如何在.NET Core中修改文件访问控制

问题描述 投票:16回答:3

我正在尝试更改.NET Core中文件的权限。但是,似乎FileInfo不再具有任何SetAccessControl

// Create a new FileInfo object.
FileInfo fInfo = new FileInfo(FileName);

// Get a FileSecurity object that represents the 
// current security settings.
FileSecurity fSecurity = fInfo.GetAccessControl();

// Add the FileSystemAccessRule to the security settings. 
fSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                                                Rights,
                                                ControlType));

// Set the new access settings.
fInfo.SetAccessControl(fSecurity);

目标只是向文件的当前所有者添加执行权(不是Windows或Unix特定功能)。>>

有关如何在.NET Core上执行此操作的任何线索?

我正在尝试更改.NET Core中文件的权限。但是,似乎FileInfo不再具有任何SetAccessControl。 //创建一个新的FileInfo对象。 FileInfo fInfo =新FileInfo(...

c# file-permissions .net-core
3个回答
18
投票

FileSecurity类现在是.NET Core System.IO.FileSystem.AccessControl程序包的一部分。不再有File.GetAccessControl方法,因此您将需要自己实例化FileSecurity实例。


4
投票

这时有两种扩展方法:GetAccessControlSetAccessControl,用于FileInfoDirectoryInfo等。>

因此您可以使用var ac = new FileInfo(path).GetAccessControl(),此表达式在.NET Framework和.Net Core中均有效。但是您仍然需要dotnet add package System.IO.FileSystem.AccessControl


3
投票

如何在Windows上获取和修改用户组的其他权限

© www.soinside.com 2019 - 2024. All rights reserved.