我正在接通。c#更改文件的权限。我得到错误信息,尽管正确的"使用"参考文献我要怎么做才能让这一切顺利呢?我还尝试Nuget以下包:system . io . filessystem . accesscontrol和System.Security.AccessControl。但是没有成功。
我的代码是这样的:
using System;
using System.IO;
using System.Security.AccessControl;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
try
{
string fileName = "test.xml";
Console.WriteLine("Adding access control entry for "
+ fileName);
// Add the access control entry to the file.
AddFileSecurity(fileName, @"DomainNameAccountName",
FileSystemRights.ReadData, AccessControlType.Allow);
Console.WriteLine("Removing access control entry from "
+ fileName);
// Remove the access control entry from the file.
RemoveFileSecurity(fileName, @"DomainNameAccountName",
FileSystemRights.ReadData, AccessControlType.Allow);
Console.WriteLine("Done.");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
// Adds an ACL entry on the specified file for the specified account.
public static void AddFileSecurity(string fileName, string account,
FileSystemRights rights, AccessControlType controlType)
{
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);
// Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);
}
// Removes an ACL entry on the specified file for the specified account.
public static void RemoveFileSecurity(string fileName, string account,
FileSystemRights rights, AccessControlType controlType)
{
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);
}
}
}
解决了,工作正常:
// Adds an ACL entry on the specified file for the specified account.
public static void AddFileSecurity(string fileName, string account,
FileSystemRights rights, AccessControlType controlType)
{
var fi1 = new FileInfo(fileName);
var fsecurity = FileSystemAclExtensions.GetAccessControl(fi1);
// Add the FileSystemAccessRule to the security settings.
fsecurity.AddAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
FileSystemAclExtensions.SetAccessControl(fi1, fsecurity);
}
按此操作
Need packagerreference " system . io . filessystem . accesscontrol "在NetCore
public static void changeFileAccess(String destination)
{
FileSecurity securite = new FileSecurity();
securite.AddAccessRule(new FileSystemAccessRule("everyone", FileSystemRights.Write | FileSystemRights.Read, AccessControlType.Allow));
#if NETSTANDARD || NETCOREAPP
FileInfo file = new FileInfo(destination);
FileSystemAclExtensions.SetAccessControl(file, securite);
#else
File.SetAccessControl(destination, securite);
#endif
}