我正在尝试授予用户访问该文件夹的权限,但是当我尝试运行该程序时,错误显示为:Some or all identity references could not be translated
。
以下是我正在使用的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Security;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Management;
using System.Management.Instrumentation;
namespace FolderLock
{
public partial class Lock : Form
{
public Lock()
{
InitializeComponent();
SetAccess();
}
private void Lock_Load(object sender, EventArgs e)
{
}
public void SetAccess()
{
DirectoryInfo myDirectoryInfo = new DirectoryInfo("C:/Users/Trov/Desktop/Test");
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
string User = System.Environment.UserDomainName + "\\" + "92111092";
myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Read, AccessControlType.Deny));
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
}
}
}
答案 0 :(得分:7)
我找到了一种方法,而不是试图允许或拒绝特定用户访问该文件夹,我只是创建一个众所周知的经过身份验证的用户来拒绝或允许它访问该文件夹。
以下是代码:
public void SetAccess()
{
DirectoryInfo myDirectoryInfo = new DirectoryInfo(@"C:/Users/Trov/Desktop/Test");
var sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(sid, FileSystemRights.Read, AccessControlType.Deny));
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
this.Hide();
this.Close();
}
谢谢