{"id":6,"date":"2015-08-14T16:38:27","date_gmt":"2015-08-14T08:38:27","guid":{"rendered":"http:\/\/www.ezhushou.cn\/?p=6"},"modified":"2015-08-14T16:38:27","modified_gmt":"2015-08-14T08:38:27","slug":"c%e8%ae%be%e7%bd%ae%e4%b8%8e%e8%8e%b7%e5%8f%96%e7%9b%ae%e5%bd%95%e6%9d%83%e9%99%90%ef%bc%88-net%e6%8e%a7%e5%88%b6acl%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.fh1986.com\/index.php\/archives\/6.html","title":{"rendered":"C#\u8bbe\u7f6e\u4e0e\u83b7\u53d6\u76ee\u5f55\u6743\u9650\uff08.net\u63a7\u5236ACL\uff09"},"content":{"rendered":"<p>&nbsp;<\/p>\n<pre class=\"csharpcode\"> <span class=\"kwrd\">static<\/span> <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">void<\/span> AddDirectorySecurity(<span class=\"kwrd\">string<\/span> FileName, <span class=\"kwrd\">string<\/span> Account, <span class=\"kwrd\">string<\/span> UserRights)\n        {\n            FileSystemRights Rights = <span class=\"kwrd\">new<\/span> FileSystemRights();\n\n            <span class=\"kwrd\">if<\/span> (UserRights.IndexOf(<span class=\"str\">\"R\"<\/span>) &gt;= 0)\n            {\n                Rights = Rights | FileSystemRights.Read;\n            }\n            <span class=\"kwrd\">if<\/span> (UserRights.IndexOf(<span class=\"str\">\"C\"<\/span>) &gt;= 0)\n            {\n                Rights = Rights | FileSystemRights.ChangePermissions;\n            }\n            <span class=\"kwrd\">if<\/span> (UserRights.IndexOf(<span class=\"str\">\"F\"<\/span>) &gt;= 0)\n            {\n                Rights = Rights | FileSystemRights.FullControl;\n            }\n            <span class=\"kwrd\">if<\/span> (UserRights.IndexOf(<span class=\"str\">\"W\"<\/span>) &gt;= 0)\n            {\n                Rights = Rights | FileSystemRights.Write;\n            }\n\n            <span class=\"kwrd\">bool<\/span> ok;\n            DirectoryInfo dInfo = <span class=\"kwrd\">new<\/span> DirectoryInfo(FileName);\n            DirectorySecurity dSecurity = dInfo.GetAccessControl();\n            InheritanceFlags iFlags = <span class=\"kwrd\">new<\/span> InheritanceFlags();\n            iFlags = InheritanceFlags.ContainerInherit |\n InheritanceFlags.ObjectInherit;\n            FileSystemAccessRule AccessRule2 = <span class=\"kwrd\">new<\/span> FileSystemAccessRule(Account, Rights, iFlags,\n PropagationFlags.None, AccessControlType.Allow);\n            dSecurity.ModifyAccessRule(AccessControlModification.Add, AccessRule2, <span class=\"kwrd\">out<\/span> ok);\n\n            dInfo.SetAccessControl(dSecurity);\n\n            <span class=\"rem\">\/\/\u5217\u51fa\u76ee\u6807\u76ee\u5f55\u6240\u5177\u6709\u7684\u6743\u9650<\/span>\n            DirectorySecurity sec = Directory.GetAccessControl(FileName, AccessControlSections.All);\n            <span class=\"kwrd\">foreach<\/span> (FileSystemAccessRule rule <span class=\"kwrd\">in<\/span> sec.GetAccessRules(<span class=\"kwrd\">true<\/span>, <span class=\"kwrd\">true<\/span>,\n <span class=\"kwrd\">typeof<\/span>(System.Security.Principal.NTAccount)))\n            {\n                Console.WriteLine(<span class=\"str\">\"----------------------------------\"<\/span>);\n                Console.WriteLine(rule.IdentityReference.Value);\n                <span class=\"kwrd\">if<\/span> ((rule.FileSystemRights &amp; FileSystemRights.Read) != 0)\n                    Console.WriteLine(rule.FileSystemRights.ToString());\n\n            }\n            Console.Read();\n        }<\/pre>\n<style type=\"text\/css\">\n.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; static public void AddDirectorySecurity(string F &hellip; <a href=\"https:\/\/www.fh1986.com\/index.php\/archives\/6.html\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">C#\u8bbe\u7f6e\u4e0e\u83b7\u53d6\u76ee\u5f55\u6743\u9650\uff08.net\u63a7\u5236ACL\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,3],"tags":[],"_links":{"self":[{"href":"https:\/\/www.fh1986.com\/index.php\/wp-json\/wp\/v2\/posts\/6"}],"collection":[{"href":"https:\/\/www.fh1986.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fh1986.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fh1986.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fh1986.com\/index.php\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":0,"href":"https:\/\/www.fh1986.com\/index.php\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.fh1986.com\/index.php\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fh1986.com\/index.php\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fh1986.com\/index.php\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}