国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - C# - 采用C#代碼動(dòng)態(tài)設(shè)置文件權(quán)限

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

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

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

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

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

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

?
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,現(xiàn)在看一下AccessRule的源碼:

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <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)限

?
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>
 ///設(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)限列表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <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);
  }
 }

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家有所幫助,同時(shí)也希望多多支持服務(wù)器之家!

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

 

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲激情在线播放 | 国产精品久久久久久久久久久久久 | 成人久久久久久久久 | 国产区在线| 亚洲一区中文字幕在线观看 | 亚洲视频在线播放免费 | 中文字幕不卡一区 | 97成人精品视频在线观看 | 午夜成人免费电影 | 久久久久久国产精品 | 中文天堂在线观看视频 | 国产成人精品综合 | 国产一区二区三区四区hd | 日韩午夜 | 欧美视频精品 | 四季久久免费一区二区三区四区 | 日韩在线电影 | 日本黄色激情片 | 亚洲视频在线免费观看 | 色网站在线观看 | 亚洲好色视频 | 久久久高清 | 日韩在线免费电影 | 久久久久久久久国产 | 久久久免费国产 | 最新日韩av | 精品日韩一区二区 | 久久久精品网站 | 懂色av一区二区三区免费观看 | 国产一区二区三区四区五区密私 | 亚洲狼人 | 综合精品| 免费精品 | 另类视频网站 | 精品国产91 | 亚洲综合色自拍一区 | 午夜成人免费视频 | 欧美精品在线一区二区 | 99久久久国产精品 | 伊人网视频在线 | 日日干夜夜干 |