天天看點

c# 設定檔案夾權限

string sPath = Server.MapPath(檔案夾名稱字元串);

Directory.CreateDirectory(sPath);

addpathPower(sPath, "ASPNET", "FullControl");

//

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(uername, 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;

    }

    dirinfo.SetAccessControl(dirsecurity);

}

本文來自CSDN部落格,轉載請标明出處:http://blog.csdn.net/LiveStar/archive/2008/07/20/2680734.aspx