欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

采用C#代碼動態(tài)設(shè)置文件權(quán)限

 更新時間:2016年12月05日 16:00:56   作者:彭澤0902  
在開發(fā)中,我們經(jīng)常會使用IO操作,例如創(chuàng)建,刪除文件等操作。在項目中這樣的需求也較多,我們也會經(jīng)常對這些操作進行編碼,但是對文件的權(quán)限進行設(shè)置,這樣的操作可能會手動操作,本文介紹一種采用代碼動態(tài)對文件設(shè)置權(quán)限的操作。

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

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

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

 public FileSystemAccessRule(
   IdentityReference identity,
   FileSystemRights fileSystemRights,
   AccessControlType type )
   : this(
    identity,
    AccessMaskFromRights( fileSystemRights, type ),
    false,
    InheritanceFlags.None,
    PropagationFlags.None,
    type )
  {
  }
  public FileSystemAccessRule(
   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 FileSystemAccessRule(
   IdentityReference identity,
   FileSystemRights fileSystemRights,
   InheritanceFlags inheritanceFlags,
   PropagationFlags propagationFlags,
   AccessControlType type )
   : this(
    identity,
    AccessMaskFromRights( fileSystemRights, type ),
    false,
    inheritanceFlags,
    propagationFlags,
    type )
  {
  }
  public FileSystemAccessRule(
   String identity,
   FileSystemRights fileSystemRights,
   InheritanceFlags inheritanceFlags,
   PropagationFlags propagationFlags,
   AccessControlType type )
   : this(
    new NTAccount(identity),
    AccessMaskFromRights( fileSystemRights, type ),
    false,
    inheritanceFlags,
    propagationFlags,
    type )
  {
  }
  internal FileSystemAccessRule(
   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.由于FileSystemAccessRule繼承自AccessRule,現(xiàn)在看一下AccessRule的源碼:

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

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

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

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

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

 /// <summary>
  ///設(shè)置目錄權(quán)限
  /// </summary>
  /// <param name="path">目錄的路徑。</param>
  /// <param name="permission">在目錄上設(shè)置的權(quán)限。</param>
  /// <returns>指示是否在目錄上應(yīng)用權(quán)限的值。</returns>
  public bool SetDirectoryPermission(string path, FileSystemRights permission)
  {
   try
   {
    if (!DirectoryExists(path))
     return false;
    var accessRule = new FileSystemAccessRule("Users", permission,
           InheritanceFlags.None,
           PropagationFlags.NoPropagateInherit,
           AccessControlType.Allow);

    var info = new DirectoryInfo(path);
    var security = info.GetAccessControl(AccessControlSections.Access);
    bool result;
    security.ModifyAccessRule(AccessControlModification.Set, accessRule, out result);
    if (!result)
     return false;
    const InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
    accessRule = new FileSystemAccessRule("Users", permission,
           iFlags,
           PropagationFlags.InheritOnly,
           AccessControlType.Allow);

    security.ModifyAccessRule(AccessControlModification.Add, accessRule, out result);
    if (!result)
     return false;
    info.SetAccessControl(security);
    return true;
   }
   catch (Exception e)
   {
    throw new Exception(e.Message, e);
   }
  }

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

 /// <summary>
  /// 設(shè)置目錄權(quán)限列表
  /// </summary>
  /// <param name="path">目錄的路徑。</param>
  /// <param name="permissions">在目錄上設(shè)置的權(quán)限。</param>
  /// <returns>指示是否在目錄上應(yīng)用權(quán)限的值。</returns>
  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è)置操作的一個簡單介紹。

以上就是本文的全部內(nèi)容,希望對大家有所幫助,同時也希望多多支持腳本之家!

相關(guān)文章

  • C#Url操作類封裝、仿Node.Js中的Url模塊實例

    C#Url操作類封裝、仿Node.Js中的Url模塊實例

    這篇文章主要介紹了C#Url操作類封裝、仿Node.Js中的Url模塊,實例分析了C#Url操作類封裝的技巧,非常具有實用價值,需要的朋友可以參考下。
    2016-10-10
  • C#使用哈希表實現(xiàn)XML文件查詢

    C#使用哈希表實現(xiàn)XML文件查詢

    這篇文章主要為大家詳細(xì)介紹了C#如何使用哈希表實現(xiàn)XML文件查詢功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以參考下
    2024-02-02
  • 利用Aspose.Word控件實現(xiàn)Word文檔的操作

    利用Aspose.Word控件實現(xiàn)Word文檔的操作

    偶然一次機會,一個項目的報表功能指定需要導(dǎo)出為Word文檔,因此尋找了很多篇文章,不過多數(shù)介紹的比較簡單一點,于是也參考了官方的幫助介紹,終于滿足了客戶的需求。下面我由淺入深來介紹這個控件在實際業(yè)務(wù)中的使用過程吧
    2013-05-05
  • C# 使用AE獲取feature的屬性及字段操作

    C# 使用AE獲取feature的屬性及字段操作

    這篇文章主要介紹了C# 使用AE獲取feature的屬性及字段操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 清除aspx頁面緩存的程序?qū)崿F(xiàn)方法

    清除aspx頁面緩存的程序?qū)崿F(xiàn)方法

    這篇文章主要介紹了清除aspx頁面緩存的程序?qū)崿F(xiàn)方法,非常實用,需要的朋友可以參考下
    2014-08-08
  • C#中無邊框窗體移動的簡單實例

    C#中無邊框窗體移動的簡單實例

    拖動無邊框窗體Form至桌面任何位置,有需要的朋友可以參考一下
    2013-08-08
  • C#泛型運作原理的深入理解

    C#泛型運作原理的深入理解

    這篇文章主要給大家介紹了關(guān)于C#泛型運作原理的深入理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • C#實現(xiàn)用于操作wav聲音文件的類實例

    C#實現(xiàn)用于操作wav聲音文件的類實例

    這篇文章主要介紹了C#實現(xiàn)用于操作wav聲音文件的類,實例分析了C#操作wav音頻文件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • C#中使用IrisSkin2.dll美化WinForm程序界面的方法

    C#中使用IrisSkin2.dll美化WinForm程序界面的方法

    這篇文章主要介紹了c#中使用IrisSkin2.dll美化WinForm程序界面的實現(xiàn)方法,需要的朋友可以參考下
    2013-05-05
  • DataGridView實現(xiàn)點擊列頭升序和降序排序

    DataGridView實現(xiàn)點擊列頭升序和降序排序

    這篇文章介紹了DataGridView實現(xiàn)點擊列頭升序和降序排序的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02

最新評論