激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - 采用C#代碼動態設置文件權限

采用C#代碼動態設置文件權限

2021-12-13 15:09彭澤0902 C#

在開發中,我們經常會使用IO操作,例如創建,刪除文件等操作。在項目中這樣的需求也較多,我們也會經常對這些操作進行編碼,但是對文件的權限進行設置,這樣的操作可能會手動操作,本文介紹一種采用代碼動態對文件設置權

在開發中,我們經常會使用IO操作,例如創建,刪除文件等操作。在項目中這樣的需求也較多,我們也會經常對這些操作進行編碼,但是對文件的權限進行設置,這樣的操作可能會手動操作,現在介紹一種采用代碼動態對文件設置權限的操作。

   在對文件進行權限設置在DOtNet中,會采用FileSystemAccessRule類進行文件的權限操作。

    1.現在看一下FileSystemAccessRule的實現代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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,現在看一下AccessRule的源碼:

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

   看來DotNet中實現文件權限設置的操作的類,現在提供幾個具體的文件設置操作代碼:

   3.獲取目錄權限列表:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <summary>
 /// 獲取目錄權限列表
 /// </summary>
 /// <param name="path">目錄的路徑。</param>
 /// <returns>指示目錄的權限列表</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.設置目錄權限

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/// <summary>
 ///設置目錄權限
 /// </summary>
 /// <param name="path">目錄的路徑。</param>
 /// <param name="permission">在目錄上設置的權限。</param>
 /// <returns>指示是否在目錄上應用權限的值。</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.設置目錄權限列表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <summary>
 /// 設置目錄權限列表
 /// </summary>
 /// <param name="path">目錄的路徑。</param>
 /// <param name="permissions">在目錄上設置的權限。</param>
 /// <returns>指示是否在目錄上應用權限的值。</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);
  }
 }

  以上是對文件權限設置操作的一個簡單介紹。

以上就是本文的全部內容,希望對大家有所幫助,同時也希望多多支持服務器之家!

原文鏈接:http://www.cnblogs.com/pengze0902/p/5988595.html

 

延伸 · 閱讀

精彩推薦
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
主站蜘蛛池模板: 成人国产精品一区二区毛片在线 | 在线观看中文字幕国产 | 欧美aⅴ在线观看 | 国产黄色网 | 性爱视频免费 | 一区二区三区日韩视频在线观看 | 国产亚洲精品精 | 午夜a狂野欧美一区二区 | 成人午夜免费国产 | 法国性经典xxxhd| 中文字幕在线免费看 | 日韩欧美中文字幕视频 | 国产成人精品免高潮在线观看 | 三级xxxx| 日本黄色一级电影 | 人人看人人艹 | 亚洲成人免费电影 | 国产欧美精品一区二区三区四区 | 毛片av网 | 亚洲精品一区二区三区在线看 | 日日爱夜夜操 | 国产精品视频一区二区三区四 | 免费一级特黄毛片视频 | 久久嗨 | 毛片视频免费观看 | 免费看a级片 | 欧美性videofree精品 | 精品一区二区三区日本 | 草草久 | 久久精精 | 日本高清一级片 | 亚洲精品成人18久久久久 | 视频一区二区在线观看 | 国产91影院| 精品久久久久久久久久久下田 | 久久9久久 | 二区三区四区视频 | 国产一级毛片高清 | 97干色| 中文字幕在线观看电影 | 亚洲午夜精选 |