C#中如何给文件夹授予权限?

发布于 2024-11-09 11:20:23 字数 787 浏览 0 评论 0原文

我需要使用 c# 为文件夹“临时 ASP.NET 文件”授予写入权限...并且我使用此代码为其提供访问权限

DirectoryInfo d1 = new DirectoryInfo(Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "Temporary ASP.NET Files"));
DirectorySecurity md1 = d1.GetAccessControl();


string user_1 = fa.TextGuestDomain + "\\" + fa.TextGuestUser;
md1.AddAccessRule(new FileSystemAccessRule(user_1, FileSystemRights.FullControl,InheritanceFlags.ObjectInherit,PropagationFlags.InheritOnly, AccessControlType.Allow));

d1.SetAccessControl(md1);

当我在实施后检查文件夹“临时 ASP.NET 文件”的安全属性时代码中,它没有检查“写入”权限复选框,而是检查了“特殊权限”...我注意到,即使我将访问权限从写入更改为完全控制或读取,它检查了“特殊权限”一项...

这不是问题:),问题是它没有给予我赋予它的正确访问权限...当我给它写时,它的行为并不像我那样给它写权限。我不知道为什么!我这样做的方式不对吗?

笔记: 当我以手动方式进行操作时,它可以工作,而当使用编码方式时。它不起作用...

我希望你能帮助我...

非常感谢

I need to give the folder "Temporary ASP.NET Files" a write permission using c#... and I use this code to give it the access

DirectoryInfo d1 = new DirectoryInfo(Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "Temporary ASP.NET Files"));
DirectorySecurity md1 = d1.GetAccessControl();


string user_1 = fa.TextGuestDomain + "\\" + fa.TextGuestUser;
md1.AddAccessRule(new FileSystemAccessRule(user_1, FileSystemRights.FullControl,InheritanceFlags.ObjectInherit,PropagationFlags.InheritOnly, AccessControlType.Allow));

d1.SetAccessControl(md1);

When I checked for the security properties for the folder "Temporary ASP.NET Files" after implementing the code, it didn't checked the "write" permission check-box, instead of that it checked the "special permissions" one... I have noticed that even when I changed the access from write to full control or read , it checked the "special permissions" one....

This is not the problem :), the problem is its not giving the right access that i give to it... when I give it write, it doesn't act like if I give it the write permission. I don't know why !! Am I doing it the wrong way ??

Note:
when I'm doing it in the manual way its working, while when using the coding way. it's not working...

I hope you can help me with that...

Thanks alot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2024-11-16 11:20:23

我知道您的痛苦 - 文件系统 ACL 修改起来很痛苦,即使它看起来有效,在某些情况下也可能会崩溃。幸运的是,就您的情况而言,有一个简单的解决方案。

问题出在 PropagationFlags.InheritOnly 上。这意味着此权限应用于继承权限的项目 - 例如,您仅向此目录中的文件授予权限,而不是任何子目录中的文件

要授予“正常”继承的目录权限(即传播到子目录和所有文件),请对 InheritanceFlags 和 PropagationFlags 使用以下值:
InheritanceFlags.ContainerInherit | InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInheritPropagationFlags.None

I know your pain - filesystem ACLs are a pain to modify and even if it seems to work, it might break in some circumstances. In your case, there's a simple solution, fortunately.

The problem lies with PropagationFlags.InheritOnly. This means that this permission is only applied to items that inherit permissions - e.g. you are granting rights only for the files in this directory and not in any subdirectories.

To grant directory rights that inherit "normally" (i.e. propagate to subdirectories and all files), use the following values for InheritanceFlags and PropagationFlags:
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit and PropagationFlags.None.

遇见了你 2024-11-16 11:20:23
private static void GrantAccess(string file)
{
    bool exists = System.IO.Directory.Exists(file);
    if (!exists)
    {
        DirectoryInfo di = System.IO.Directory.CreateDirectory(file);
        Console.WriteLine("The Folder is created Sucessfully");
    }
    else
    {
        Console.WriteLine("The Folder already exists");
    }
    DirectoryInfo dInfo = new DirectoryInfo(file);
    DirectorySecurity dSecurity = dInfo.GetAccessControl();
    dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
    dInfo.SetAccessControl(dSecurity);
   
}

上面的代码将把文件夹的访问权限设置为每个用户(所有人)的完全控制/读写。

private static void GrantAccess(string file)
{
    bool exists = System.IO.Directory.Exists(file);
    if (!exists)
    {
        DirectoryInfo di = System.IO.Directory.CreateDirectory(file);
        Console.WriteLine("The Folder is created Sucessfully");
    }
    else
    {
        Console.WriteLine("The Folder already exists");
    }
    DirectoryInfo dInfo = new DirectoryInfo(file);
    DirectorySecurity dSecurity = dInfo.GetAccessControl();
    dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
    dInfo.SetAccessControl(dSecurity);
   
}

The above code will set the access rights of the folder to full control/ read-write to every user (everyone).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文