1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      C#設(shè)置文件權(quán)限-創(chuàng)新互聯(lián)

       在開發(fā)中,我們經(jīng)常會使用IO操作,例如創(chuàng)建,刪除文件等操作。在項目中這樣的需求也較多,我們也會經(jīng)常對這些操作進(jìn)行編碼,但是對文件的權(quán)限進(jìn)行設(shè)置,這樣的操作可能會手動操作,現(xiàn)在介紹一種采用代碼動態(tài)對文件設(shè)置權(quán)限的操作。

      創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元灤平做網(wǎng)站,已為上家服務(wù),為灤平各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108

       在對文件進(jìn)行權(quán)限設(shè)置在DOtNet中,會采用FileSystemAcce***ule類進(jìn)行文件的權(quán)限操作。

        1.現(xiàn)在看一下FileSystemAcce***ule的實現(xiàn)代碼:

      public FileSystemAcce***ule(
                  IdentityReference identity,
                  FileSystemRights fileSystemRights,
                  AccessControlType type )
                  : this(
                      identity,
                      AccessMaskFromRights( fileSystemRights, type ),
                      false,
                      InheritanceFlags.None,
                      PropagationFlags.None,
                      type )
              {
              }
      
              public FileSystemAcce***ule(
                  String identity,
                  FileSystemRights fileSystemRights,
                  AccessControlType type )
                  : this(
                      new NTAccount(identity),
                      AccessMaskFromRights( fileSystemRights, type ),
                      false,
                      InheritanceFlags.None,
                      PropagationFlags.None,
                      type )
              {
              }
      
              //
              // Constructor for creating access rules for folder objects
              //
      
              public FileSystemAcce***ule(
                  IdentityReference identity,
                  FileSystemRights fileSystemRights,
                  InheritanceFlags inheritanceFlags,
                  PropagationFlags propagationFlags,
                  AccessControlType type )
                  : this(
                      identity,
                      AccessMaskFromRights( fileSystemRights, type ),
                      false,
                      inheritanceFlags,
                      propagationFlags,
                      type )
              {
              }
      
              public FileSystemAcce***ule(
                  String identity,
                  FileSystemRights fileSystemRights,
                  InheritanceFlags inheritanceFlags,
                  PropagationFlags propagationFlags,
                  AccessControlType type )
                  : this(
                      new NTAccount(identity),
                      AccessMaskFromRights( fileSystemRights, type ),
                      false,
                      inheritanceFlags,
                      propagationFlags,
                      type )
              {
              }
              internal FileSystemAcce***ule(
                  IdentityReference identity,
                  int accessMask,
                  bool isInherited,
                  InheritanceFlags inheritanceFlags,
                  PropagationFlags propagationFlags,
                  AccessControlType type )
                  : base(
                      identity,
                      accessMask,
                      isInherited,
                      inheritanceFlags,
                      propagationFlags,
                      type )
              {
              }
      
              #endregion
      
              #region Public properties
      
              public FileSystemRights FileSystemRights
              {
                  get { return RightsFromAccessMask( base.AccessMask ); }
              }
      
       
              internal static int AccessMaskFromRights( FileSystemRights fileSystemRights, AccessControlType controlType )
              {
                  if (fileSystemRights < (FileSystemRights) 0 || fileSystemRights > FileSystemRights.FullControl)
                      throw new ArgumentOutOfRangeException("fileSystemRights", Environment.GetResourceString("Argument_InvalidEnumValue", fileSystemRights, "FileSystemRights"));
                  Contract.EndContractBlock();
      
                  if (controlType == AccessControlType.Allow) {
                      fileSystemRights |= FileSystemRights.Synchronize;
                  }
                  else if (controlType == AccessControlType.Deny) {
                      if (fileSystemRights != FileSystemRights.FullControl &&
                          fileSystemRights != (FileSystemRights.FullControl & ~FileSystemRights.DeleteSubdirectoriesAndFiles))
                          fileSystemRights &= ~FileSystemRights.Synchronize;
                  }
      
                  return ( int )fileSystemRights;
              }
      
              internal static FileSystemRights RightsFromAccessMask( int accessMask )
              {
                  return ( FileSystemRights )accessMask;
              }
      
          }

         2.由于FileSystemAcce***ule繼承自Acce***ule,現(xiàn)在看一下Acce***ule的源碼:

        /// 
        /// 表示用戶的標(biāo)識、訪問掩碼和訪問控制類型(允許或拒絕)的組合。 對象還包含有關(guān)子對象如何繼承規(guī)則以及如何傳播繼承的信息。
        /// 
        public abstract class Acce***ule : AuthorizationRule
        {
          /// 
          /// 使用指定的值初始化  類的一個新實例。
          /// 
          /// 應(yīng)用訪問規(guī)則的標(biāo)識。此參數(shù)必須是可以強(qiáng)制轉(zhuǎn)換為  的對象。此規(guī)則的訪問掩碼。訪問掩碼是一個 32 位的匿名位集合,其含義是由每個集成器定義的。如果此規(guī)則繼承自父容器,則為 true。訪問規(guī)則的繼承屬性。繼承的訪問規(guī)則是否自動傳播。如果  設(shè)置為 ,則將忽略傳播標(biāo)志。有效的訪問控制類型。 參數(shù)的值不能強(qiáng)制轉(zhuǎn)換為 ,或者  參數(shù)包含無效值。 參數(shù)的值為零,或者  或  參數(shù)包含無法識別的標(biāo)志值。
          protected Acce***ule(IdentityReference identity, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type);
          /// 
          /// 獲取與此  對象關(guān)聯(lián)的  對象。
          /// 
          /// 
          /// 
          /// 與此  對象關(guān)聯(lián)的  對象。
          /// 
          public AccessControlType AccessControlType { get; }
        }

       看了DotNet中實現(xiàn)文件權(quán)限設(shè)置的操作的類,現(xiàn)在提供幾個具體的文件設(shè)置操作代碼:

       3.獲取目錄權(quán)限列表:

              /// 
               /// 獲取目錄權(quán)限列表
              /// 
              /// 目錄的路徑。
              /// 指示目錄的權(quán)限列表
              public IList GetDirectoryPermission(string path)
              {
                  try
                  {
                      if (!DirectoryExists(path))
                          return null;
      
                      IList result = new List();
                      var dSecurity = Directory.GetAccessControl(new DirectoryInfo(path).FullName);
                      foreach (FileSystemAcce***ule rule in dSecurity.GetAcce***ules(true, true, typeof(NTAccount)))
                          result.Add(rule.FileSystemRights);
      
                      return result;
                  }
                  catch (Exception e)
                  {
                      throw new Exception(e.Message, e);
                  }
              }

        4.設(shè)置目錄權(quán)限

              /// 
              ///設(shè)置目錄權(quán)限
              /// 
              /// 目錄的路徑。
              /// 在目錄上設(shè)置的權(quán)限。
              /// 指示是否在目錄上應(yīng)用權(quán)限的值。
              public bool SetDirectoryPermission(string path, FileSystemRights permission)
              {
                  try
                  {
                      if (!DirectoryExists(path))
                          return false;
      
                      var acce***ule = new FileSystemAcce***ule("Users", permission,
                                                  InheritanceFlags.None,
                                                  PropagationFlags.NoPropagateInherit,
                                                  AccessControlType.Allow);
      
                      var info = new DirectoryInfo(path);
                      var security = info.GetAccessControl(AccessControlSections.Access);
      
                      bool result;
                      security.ModifyAcce***ule(AccessControlModification.Set, acce***ule, out result);
      
                      if (!result)
                          return false;
      
                      const InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
      
                      acce***ule = new FileSystemAcce***ule("Users", permission,
                                                  iFlags,
                                                  PropagationFlags.InheritOnly,
                                                  AccessControlType.Allow);
      
                      security.ModifyAcce***ule(AccessControlModification.Add, acce***ule, out result);
      
                      if (!result)
                          return false;
      
                      info.SetAccessControl(security);
      
                      return true;
                  }
                  catch (Exception e)
                  {
                      throw new Exception(e.Message, e);
                  }
              }

       5.設(shè)置目錄權(quán)限列表

              /// 
              /// 設(shè)置目錄權(quán)限列表
              /// 
              /// 目錄的路徑。
              /// 在目錄上設(shè)置的權(quán)限。
              /// 指示是否在目錄上應(yīng)用權(quán)限的值。
              public bool SetDirectoryPermissions(string path, FileSystemRights[] permissions)
              {
                  try
                  {
                      if (!DirectoryExists(path) || permissions == null || !permissions.Any())
                          return false;
      
                      foreach (var permission in permissions)
                          if (!SetDirectoryPermission(path, permission))
                              return false;
      
                      return true;
                  }
                  catch (Exception e)
                  {
                      throw new Exception(e.Message, e);
                  }
              }

         以上是對文件權(quán)限設(shè)置操作的一個簡單介紹。

      創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。


      本文標(biāo)題:C#設(shè)置文件權(quán)限-創(chuàng)新互聯(lián)
      本文來源:http://www.ef60e0e.cn/article/cdecdh.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        长阳| 四子王旗| 盐山县| 辽源市| 丹阳市| 扎囊县| 土默特左旗| 交城县| 凤台县| 淮南市| 厦门市| 曲阜市| 韶关市| 大理市| 乌拉特中旗| 长治市| 邻水| 卢龙县| 岑巩县| 武乡县| 新源县| 普陀区| 罗定市| 黄平县| 榆中县| 兴文县| 临安市| 扎兰屯市| 洱源县| 察哈| 资源县| 石楼县| 肃北| 剑阁县| 泸定县| 宜兰市| 永宁县| 阳春市| 富川| 石林| 保德县|