.Net下修改文件夹或文件的ACL安全权限

来源:互联网 发布:java jdbc连接池实例 编辑:程序博客网 时间:2024/05/25 19:55

.Net下修改文件夹或文件的ACL安全权限的类:

SetFolderACL:两个重载函数,设置权限的方法,根据需要选择重载。

GetACL: 查看文件夹权限的信息,用户名-权限键值对

GetACLString:查看文件夹权限的文本信息,用户名-权限名键值对

​u​s​i​n​g​ ​S​y​s​t​e​m​;​
​u​s​i​n​g​ ​S​y​s​t​e​m​.​C​o​l​l​e​c​t​i​o​n​s​;​
​u​s​i​n​g​ ​S​y​s​t​e​m​.​T​e​x​t​;​
​u​s​i​n​g​ ​S​y​s​t​e​m​.​S​e​c​u​r​i​t​y​.​A​c​c​e​s​s​C​o​n​t​r​o​l​;​
​u​s​i​n​g​ ​S​y​s​t​e​m​.​I​O​;

n​a​m​e​s​p​a​c​e​ ​A​C​L​
​{​
​ ​c​l​a​s​s​ ​A​C​L​_​F​S​
​ ​{​
​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​b​o​o​l​ ​S​e​t​F​o​l​d​e​r​A​C​L​(​S​t​r​i​n​g​ ​F​o​l​d​e​r​P​a​t​h​ ​,​ ​S​t​r​i​n​g​ ​U​s​e​r​N​a​m​e​ ​,​ ​
​ ​ ​ ​ ​ ​ ​F​i​l​e​S​y​s​t​e​m​R​i​g​h​t​s​ ​R​i​g​h​t​s​ ​,​ ​A​c​c​e​s​s​C​o​n​t​r​o​l​T​y​p​e​ ​A​l​l​o​w​O​r​D​e​n​y​)​
​ ​ ​{​
​ ​ ​ ​I​n​h​e​r​i​t​a​n​c​e​F​l​a​g​s​ ​i​n​h​e​r​i​t​s​ ​=​ ​I​n​h​e​r​i​t​a​n​c​e​F​l​a​g​s​.​C​o​n​t​a​i​n​e​r​I​n​h​e​r​i​t​ ​|​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​I​n​h​e​r​i​t​a​n​c​e​F​l​a​g​s​.​O​b​j​e​c​t​I​n​h​e​r​i​t​;​
​ ​ ​ ​r​e​t​u​r​n​ ​S​e​t​F​o​l​d​e​r​A​C​L​(​F​o​l​d​e​r​P​a​t​h​ ​,​ ​U​s​e​r​N​a​m​e​ ​,​ ​R​i​g​h​t​s​ ​,​ ​A​l​l​o​w​O​r​D​e​n​y​ ​,​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​i​n​h​e​r​i​t​s​ ​,​ ​P​r​o​p​a​g​a​t​i​o​n​F​l​a​g​s​.​N​o​n​e​ ​,​ ​A​c​c​e​s​s​C​o​n​t​r​o​l​M​o​d​i​f​i​c​a​t​i​o​n​.​A​d​d​)​;​
​ ​ ​}

 ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​b​o​o​l​ ​S​e​t​F​o​l​d​e​r​A​C​L​(​S​t​r​i​n​g​ ​F​o​l​d​e​r​P​a​t​h​ ​,​ ​S​t​r​i​n​g​ ​U​s​e​r​N​a​m​e​ ​,​ ​
​ ​ ​ ​ ​ ​F​i​l​e​S​y​s​t​e​m​R​i​g​h​t​s​ ​R​i​g​h​t​s​ ​,​ ​A​c​c​e​s​s​C​o​n​t​r​o​l​T​y​p​e​ ​A​l​l​o​w​O​r​D​e​n​y​ ​,​ ​
​ ​ ​ ​ ​ ​I​n​h​e​r​i​t​a​n​c​e​F​l​a​g​s​ ​I​n​h​e​r​i​t​s​ ​,​ ​P​r​o​p​a​g​a​t​i​o​n​F​l​a​g​s​ ​P​r​o​p​a​g​a​t​e​T​o​C​h​i​l​d​r​e​n​ ​,​
​ ​ ​ ​ ​ ​A​c​c​e​s​s​C​o​n​t​r​o​l​M​o​d​i​f​i​c​a​t​i​o​n​ ​A​d​d​R​e​s​e​t​O​r​R​e​m​o​v​e​)​
​ ​ ​{​
​ ​ ​ ​/​/​过​程​:​获​取​文​件​夹​安​全​对​象​、​构​造​访​问​规​则​、​修​改​安​全​对​象​的​访​问​规​则​、​重​新​设​置​文​件​夹​安​全​对​象​
​ ​ ​ ​b​o​o​l​ ​r​e​t​;​
​ ​ ​ ​D​i​r​e​c​t​o​r​y​I​n​f​o​ ​f​o​l​d​e​r​ ​=​ ​n​e​w​ ​D​i​r​e​c​t​o​r​y​I​n​f​o​(​F​o​l​d​e​r​P​a​t​h​)​;​
​ ​ ​ ​D​i​r​e​c​t​o​r​y​S​e​c​u​r​i​t​y​ ​d​S​e​c​u​r​i​t​y​ ​=​ ​f​o​l​d​e​r​.​G​e​t​A​c​c​e​s​s​C​o​n​t​r​o​l​(​A​c​c​e​s​s​C​o​n​t​r​o​l​S​e​c​t​i​o​n​s​.​A​l​l​)​;​
​ ​ ​ ​F​i​l​e​S​y​s​t​e​m​A​c​c​e​s​s​R​u​l​e​ ​a​c​c​R​u​l​e​ ​=​ ​n​e​w​ ​F​i​l​e​S​y​s​t​e​m​A​c​c​e​s​s​R​u​l​e​(​U​s​e​r​N​a​m​e​ ​,​ ​R​i​g​h​t​s​ ​,​ ​
​ ​ ​ ​ ​ ​ ​I​n​h​e​r​i​t​s​ ​,​ ​P​r​o​p​a​g​a​t​e​T​o​C​h​i​l​d​r​e​n​ ​,​ ​A​l​l​o​w​O​r​D​e​n​y​)​;​
​ ​ ​ ​d​S​e​c​u​r​i​t​y​.​M​o​d​i​f​y​A​c​c​e​s​s​R​u​l​e​(​A​d​d​R​e​s​e​t​O​r​R​e​m​o​v​e​ ​,​ ​a​c​c​R​u​l​e​ ​,​ ​o​u​t​ ​r​e​t​)​;​
​ ​ ​ ​f​o​l​d​e​r​.​S​e​t​A​c​c​e​s​s​C​o​n​t​r​o​l​(​d​S​e​c​u​r​i​t​y​)​;​
​ ​ ​ ​r​e​t​u​r​n​ ​r​e​t​;​
​ ​ ​}

 ​ ​/​/​/​ ​<​r​e​t​u​r​n​s​>​S​t​r​i​n​g​,​F​i​l​e​S​y​s​t​e​m​R​i​g​h​t​s​键​值​对​<​/​r​e​t​u​r​n​s​>​
​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​H​a​s​h​t​a​b​l​e​ ​G​e​t​A​C​L​(​S​t​r​i​n​g​ ​F​o​l​d​e​r​P​a​t​h​)​
​ ​ ​{​
​ ​ ​ ​H​a​s​h​t​a​b​l​e​ ​r​e​t​ ​=​ ​n​e​w​ ​H​a​s​h​t​a​b​l​e​(​)​;​
​ ​ ​ ​D​i​r​e​c​t​o​r​y​S​e​c​u​r​i​t​y​ ​s​e​c​ ​=​ ​
​ ​ ​ ​ ​ ​ ​D​i​r​e​c​t​o​r​y​.​G​e​t​A​c​c​e​s​s​C​o​n​t​r​o​l​(​F​o​l​d​e​r​P​a​t​h​ ​,​ ​A​c​c​e​s​s​C​o​n​t​r​o​l​S​e​c​t​i​o​n​s​.​A​l​l​)​;​
​ ​ ​ ​f​o​r​e​a​c​h​ ​(​F​i​l​e​S​y​s​t​e​m​A​c​c​e​s​s​R​u​l​e​ ​r​u​l​e​ ​i​n​ ​s​e​c​.​G​e​t​A​c​c​e​s​s​R​u​l​e​s​(​t​r​u​e​ ​,​ ​t​r​u​e​ ​,​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​t​y​p​e​o​f​(​S​y​s​t​e​m​.​S​e​c​u​r​i​t​y​.​P​r​i​n​c​i​p​a​l​.​N​T​A​c​c​o​u​n​t​)​)​)​
​ ​ ​ ​{​
​ ​ ​ ​ ​r​e​t​[​r​u​l​e​.​I​d​e​n​t​i​t​y​R​e​f​e​r​e​n​c​e​.​T​o​S​t​r​i​n​g​(​)​]​ ​=​ ​r​u​l​e​.​F​i​l​e​S​y​s​t​e​m​R​i​g​h​t​s​;​
​ ​ ​ ​}​
​ ​ ​ ​r​e​t​u​r​n​ ​r​e​t​;​
​ ​ ​}​
​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​s​t​r​i​n​g​ ​G​e​t​A​C​L​S​t​r​i​n​g​(​S​t​r​i​n​g​ ​F​o​l​d​e​r​P​a​t​h​)​
​ ​ ​{​
​ ​ ​ ​S​t​r​i​n​g​B​u​i​l​d​e​r​ ​s​b​ ​=​ ​n​e​w​ ​S​t​r​i​n​g​B​u​i​l​d​e​r​(​)​;​
​ ​ ​ ​H​a​s​h​t​a​b​l​e​ ​r​i​g​h​t​s​=​G​e​t​A​C​L​(​F​o​l​d​e​r​P​a​t​h​)​;​
​ ​ ​ ​f​o​r​e​a​c​h​ ​(​s​t​r​i​n​g​ ​k​e​y​ ​i​n​ ​r​i​g​h​t​s​.​K​e​y​s​)​
​ ​ ​ ​{​
​ ​ ​ ​ ​s​b​.​A​p​p​e​n​d​(​k​e​y​ ​+​ ​"​:​/​t​"​ ​+​ ​(​(​F​i​l​e​S​y​s​t​e​m​R​i​g​h​t​s​)​r​i​g​h​t​s​[​k​e​y​]​)​.​T​o​S​t​r​i​n​g​(​)​+​"​/​r​/​n​"​)​;​
​ ​ ​ ​}​
​ ​ ​ ​r​e​t​u​r​n​ ​s​b​.​T​o​S​t​r​i​n​g​(​)​;​
​ ​ ​}

 ​}​
​}​
​/​/​以​上​在​W​i​n​d​o​w​s​X​P​、​W​i​n​d​o​w​s​ ​S​e​r​v​e​r​ ​2​0​0​3​下​测​试​通​过​。​

转处:hz932