zoukankan      html  css  js  c++  java
  • 设置文件夹权限问题

    绝对路径:

            protected void Button1_Click(object sender, EventArgs e)
            {
                try
                {
                    string filename = @"E:\k"; //目标目录
                    string account = @"Everyone";//用户名
                    string userrights = @"F";//权限字符串,自己定义的
                    AddDirectorySecurity(filename, account, userrights);
                }
                catch (Exception ex)
                {
                }

            }

            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);
            }

    相对路径:


            protected void Button1_Click(object sender, EventArgs e)
            {
                try
                {
                    string filename = @"WebApplication1"; //(这边是根目录的名称其它可以像后面那样写)目标目录 ~/Account
                    string account = @"Everyone";//用户名
                    string userrights = @"F";//权限字符串,自己定义的
                    AddDirectorySecurity(filename, account, userrights);
                }
                catch (Exception ex)
                {
                }

            }

            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(System.Web.HttpContext.Current.Server.MapPath(FileName));
                if ((dInfo.Attributes & FileAttributes.ReadOnly) != 0)
                {
                    dInfo.Attributes = FileAttributes.Normal;
                }
                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);
            }

  • 相关阅读:
    Windows 2008 R2 安装 Windows phone 7 开发环境
    win 7,win2008 无法给新建用户完全权限
    基于Ajax的Asp.Net 简易在线聊天室
    phpwind ecshop 用户整合
    UVALive 3942 Remember the Word(字典树+DP)
    UVA 11732 strcmp() Anyone? (压缩版字典树)
    UVA 11992 Fast Matrix Operations(线段树:区间修改)
    hdu 2222 Keywords Search(AC自动机模版题)
    动态规划基础练习笔记
    递归与分治策略基础练习笔记
  • 原文地址:https://www.cnblogs.com/wujy/p/2332641.html
Copyright © 2011-2022 走看看