/// <summary>
    /// 为访问的文件分配权限
    /// </summary>
    /// <param name="pathname">文件路径</param>
    /// <param name="username">赋予的username,类似"ASPNET"</param>
    /// <param name="power">所赋予的权限</param>
    public void AddPathPower(string pathname, string username, string power)
    {
        DirectoryInfo dirinfo = new DirectoryInfo(pathname);
        if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
        {
            dirinfo.Attributes = FileAttributes.Normal;
        }
        DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
        switch (power)
        {
            case "FullControl": dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
                break;
            case "ReadOnly":
                dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));
                break;
            case "Write":
                dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));
                break;
            case "Modify":
                dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow));
                break;

 

 

 

 
        }
    }

posted on 2009-10-22 11:30  sweting  阅读(224)  评论(0编辑  收藏  举报