C#-为Windows 7中的所有用户设置目录权限
这应该是一个相当简单的问题,但由于某种原因,我似乎无法让它工作。我要做的就是设置给定目录的权限,以允许所有用户完全访问。以下是我目前掌握的代码:C#-为Windows 7中的所有用户设置目录权限,c#,permissions,directory,C#,Permissions,Directory,这应该是一个相当简单的问题,但由于某种原因,我似乎无法让它工作。我要做的就是设置给定目录的权限,以允许所有用户完全访问。以下是我目前掌握的代码: System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(destinationDirectory); FileSystemAccessRule fsar = new FileSystemAccessRule("Users", FileSystemRights.FullControl, Acces
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(destinationDirectory);
FileSystemAccessRule fsar = new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow);
DirectorySecurity ds = null;
if (!di.Exists)
{
System.IO.Directory.CreateDirectory(destinationDirectory);
}
ds = di.GetAccessControl();
ds.AddAccessRule(fsar);
没有抛出异常,但也没有发生任何事情。当我在代码运行后检查目录权限时,我没有看到任何更改
有什么想法吗?您还需要打电话应用更改
ds = di.GetAccessControl();
ds.AddAccessRule(fsar);
di.SetAccessControl(ds); // nothing happens until you do this
如前所述,MSDN上的示例似乎非常缺乏细节。我对本文中的代码进行了黑客攻击,以获得以下性能良好的代码:
static bool SetAcl()
{
FileSystemRights Rights = (FileSystemRights)0;
Rights = FileSystemRights.FullControl;
// *** Add Access Rule to the actual directory itself
FileSystemAccessRule AccessRule = new FileSystemAccessRule("Users", Rights,
InheritanceFlags.None,
PropagationFlags.NoPropagateInherit,
AccessControlType.Allow);
DirectoryInfo Info = new DirectoryInfo(destinationDirectory);
DirectorySecurity Security = Info.GetAccessControl(AccessControlSections.Access);
bool Result = false;
Security.ModifyAccessRule(AccessControlModification.Set, AccessRule, out Result);
if (!Result)
return false;
// *** Always allow objects to inherit on a directory
InheritanceFlags iFlags = InheritanceFlags.ObjectInherit;
iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
// *** Add Access rule for the inheritance
AccessRule = new FileSystemAccessRule("Users", Rights,
iFlags,
PropagationFlags.InheritOnly,
AccessControlType.Allow);
Result = false;
Security.ModifyAccessRule(AccessControlModification.Add, AccessRule, out Result);
if (!Result)
return false;
Info.SetAccessControl(Security);
return true;
}
David Heffernan answer在非英语机器上不起作用,在非英语机器上,尝试设置“用户”权限失败,出现
IdentityNotMapped
异常。通过使用WellKnownSidType.BuiltinUsersSid
,以下代码可以在任何地方使用:
static void SetFullControlPermissionsToEveryone(string path)
{
const FileSystemRights rights = FileSystemRights.FullControl;
var allUsers = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
// Add Access Rule to the actual directory itself
var accessRule = new FileSystemAccessRule(
allUsers,
rights,
InheritanceFlags.None,
PropagationFlags.NoPropagateInherit,
AccessControlType.Allow);
var info = new DirectoryInfo(path);
var security = info.GetAccessControl(AccessControlSections.Access);
bool result;
security.ModifyAccessRule(AccessControlModification.Set, accessRule, out result);
if (!result)
{
throw new InvalidOperationException("Failed to give full-control permission to all users for path " + path);
}
// add inheritance
var inheritedAccessRule = new FileSystemAccessRule(
allUsers,
rights,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.InheritOnly,
AccessControlType.Allow);
bool inheritedResult;
security.ModifyAccessRule(AccessControlModification.Add, inheritedAccessRule, out inheritedResult);
if (!inheritedResult)
{
throw new InvalidOperationException("Failed to give full-control permission inheritance to all users for " + path);
}
info.SetAccessControl(security);
}
您是否尝试在禁用UAC的情况下运行上面的代码?@David-我尝试以管理员身份运行编译后的exe,但结果没有任何差别。@rkosegi-我该怎么做?这是Visual Studio中的设置吗?@SonnyBoy不,这是一个系统范围的设置,您不应该禁用它。如果你禁用了它,那么当你的用户运行你的代码,发现它由于缺乏特权而失败时,你会大吃一惊。最好不要有这样的惊喜。谢谢你,大卫。不过还是没有运气。为此,我使用的“用户”组是否正确?我已尝试以管理员身份运行exe,但当我通过Windows检查文件夹的权限时,仍然没有发生任何事情。正在靠近。将“用户”更改为“所有人”现在将“所有人”组添加到文件夹中,但权限显示为空;没有授予任何权限。即使与
“用户”
一起使用,代码也可以正常工作。也就是说,我认为全名是@“BUILTIN\Users”
,但它是双向的。这是一个非常尴尬的代码。这一个更简单:@Elmue该解决方案对我不起作用-“所有用户”无法获得完全控制权。但是,这个答案和Timothée Lecomte的答案确实有效(显然是由于设置了2阶段权限),我是否可以更改网络文件夹,如\\server\folder1\subfolder1\subfolder2?