| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> C#应用技巧 >> 技术文摘正文
最新推荐 更多内容
C#设置与获取目录NTFS权限ACL的方法
C#设置与获取目录NTFS权限ACL的方法
作者:佚名    来源:乐博网收集     更新时间:2011-6-26

对照MSDN,很容易看懂上面的代码。C#设置与获取目录NTFS权限ACL的方法, 但是貌似这个程序需要以管理员身份来运行。^_^

其中的Directory.GetAccessControl(FileName, AccessControlSections.All);  第二个参数如果为AccessControlSections.Access ,就可以使得运行在IIS中的Web应用程序获得目录权限了。

using System;
using System.Collections;
using System.IO;
using System.Security.AccessControl;
static class Tester
{

    public static void Main()
    {
        try
        {
            string filename = @"d:\乐博网"; //目标目录
            string account = @"akyao";//用户名
            string userrights = @"R";//权限字符串,自己定义的
            AddDirectorySecurity(filename, account, userrights);
            Console.ReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            Console.ReadLine();
        }
    }

    static public void AddDirectorySecurity(string FileName, string Account, string UserRights)
    {
        FileSystemRights Rights = new FileSystemRights();

        if (UserRights.IndexOf("R") >= 0)
        {
            Rights = Rights | FileSystemRights.Read;
        }
        if (UserRights.IndexOf("C") >= 0)
        {
            Rights = Rights | FileSystemRights.ChangePermissions;
        }
        if (UserRights.IndexOf("F") >= 0)
        {
            Rights = Rights | FileSystemRights.FullControl;
        }
        if (UserRights.IndexOf("W") >= 0)
        {
            Rights = Rights | FileSystemRights.Write;
        }

        bool ok;
        DirectoryInfo dInfo = new DirectoryInfo(FileName);
        DirectorySecurity dSecurity = dInfo.GetAccessControl();
        InheritanceFlags iFlags = new InheritanceFlags();
        iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
        FileSystemAccessRule AccessRule2 = new FileSystemAccessRule(Account, Rights, iFlags, PropagationFlags.None, AccessControlType.Allow);
        dSecurity.ModifyAccessRule(AccessControlModification.Add, AccessRule2, out ok);

        dInfo.SetAccessControl(dSecurity);

        //列出目标目录所具有的权限
        DirectorySecurity sec = Directory.GetAccessControl(FileName, AccessControlSections.All);
        foreach (FileSystemAccessRule rule in sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
        {
            Console.WriteLine("----------------------------------");
            Console.WriteLine(rule.IdentityReference.Value);
            if ((rule.FileSystemRights & FileSystemRights.Read) != 0)
                Console.WriteLine(rule.FileSystemRights.ToString());

        }
        Console.Read();
    }

}

  • 上一篇:

  • 下一篇: 没有了
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    用C#代码解决SQL Server数据库附加后只读或失败
    C#设置NTFS权限的方法

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]