关闭

C#下设置文件夹权限

标签: c#buttontextboxpathserverobject
3155人阅读 评论(0) 收藏 举报

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SetDir.aspx.cs" Inherits="SetDir" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server">Uploads</asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="读权限" OnClick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="写权限" OnClick="Button2_Click" />
        <asp:Button ID="Button3" runat="server" Text="完全控制权限" OnClick="Button3_Click" />
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Security.AccessControl;

public partial class SetDir : System.Web.UI.Page
{
    public string setPath ;
    protected void Page_Load(object sender, EventArgs e)
    {
        setPath = TextBox1.Text;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Security.AccessControl.DirectorySecurity fSec;

        string path = System.Web.HttpContext.Current.Server.MapPath("~/"+setPath);
        Response.Write(path);
        //Response.End();
      
        fSec = new DirectorySecurity();
        //设置权限的应用为文件夹本身、子文件夹及文件
        //所以需要InheritanceFlags.ContainerInherit 或 InheritanceFlags.ObjectInherit
        fSec.AddAccessRule(new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.Read, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
        System.IO.Directory.SetAccessControl(path, fSec);
        Response.Write("<br/>"+path+"可读权限设置完成。");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        System.Security.AccessControl.DirectorySecurity fSec;

        string path = System.Web.HttpContext.Current.Server.MapPath("~/" + setPath);
        Response.Write(path);
        //Response.End();

        fSec = new DirectorySecurity();
        //设置权限的应用为文件夹本身、子文件夹及文件
        //所以需要InheritanceFlags.ContainerInherit 或 InheritanceFlags.ObjectInherit
        fSec.AddAccessRule(new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.Write, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
        System.IO.Directory.SetAccessControl(path, fSec);
        Response.Write("<br/>" + path + "可写权限设置完成。");

    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        System.Security.AccessControl.DirectorySecurity fSec;

        string path = System.Web.HttpContext.Current.Server.MapPath("~/" + setPath);
        Response.Write(path);
        //Response.End();

        fSec = new DirectorySecurity();
        //设置权限的应用为文件夹本身、子文件夹及文件
        //所以需要InheritanceFlags.ContainerInherit 或 InheritanceFlags.ObjectInherit
        fSec.AddAccessRule(new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
        System.IO.Directory.SetAccessControl(path, fSec);
        Response.Write("<br/>" + path + "完全控制权限设置完成。");

    }
}

转处:baidu

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:260560次
    • 积分:3275
    • 等级:
    • 排名:第10374名
    • 原创:65篇
    • 转载:79篇
    • 译文:1篇
    • 评论:36条
    最新评论