zoukankan      html  css  js  c++  java
  • Create Directory with windows access rights

    I met some troubles when tring to create directory with access rights, fortunately, I found some useful imformation, just mark them as below,

    View Code
    Imports System.Security.AccessControl

    Imports System.IO



    Public Class Form1



    ' Add access control for Windows Account to directory or file

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    AddFileSecurity(
    "\\MartinXie\ShareFolder", "Everyone", "Write")

    AddFileSecurity(
    "\\MartinXie\ShareFolder", "Domain\UserCccount", "FullControl")

    End Sub



    Public Sub AddFileSecurity(ByVal filePath As String, ByVal username As String, ByVal power As String)

    Dim dirinfo As DirectoryInfo
    = New DirectoryInfo(filePath)

    Dim dirsecurity As DirectorySecurity
    = dirinfo.GetAccessControl()

    Select Case power

    Case
    "FullControl"

    dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))

    dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow))

    dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))

    Case
    "ReadOnly"

    dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow))

    Case
    "Write"

    dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))

    dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow))

    dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))

    Case
    "Modify"

    dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow))

    End Select

    dirinfo.SetAccessControl(dirsecurity)

    End Sub

    End Class

    If you want to remove access control permission for Windows Account from directory or file, just change AccessControlType.Allow to AccessControlType.Deny in above code sample.

     

    Some tutorials:
    Article:Working with Access Control List in .NET

    http://nayyeri.net/blog/Working-with-Access-Control-List-in-NET-2-0/

    Thread: about DirectorySecurity discussion
    http://social.msdn.microsoft.com/Forums/en-US/clr/thread/a83ddfea-5ad7-4a44-ab40-0fd5cadf4e99

  • 相关阅读:
    LUOGU P1654 OSU! (概率期望)
    poj 3682 King Arthur's Birthday Celebration (期望dp)
    CF148D Bag of mice (期望dp)
    LUOGU P1514 引水入城 (bfs)
    LUOGU P4281 [AHOI2008]紧急集合 / 聚会 (lca)
    LUOGU P1313 计算系数 (组合数学)
    LUOGU P2949 [USACO09OPEN]工作调度Work Scheduling (贪心)
    LUOGU P1613 跑路 (倍增floyd)
    LUOGU P1291 [SHOI2002]百事世界杯之旅 (期望dp)
    poj 3208--Apocalypse Someday(数位dp)
  • 原文地址:https://www.cnblogs.com/chenjunsheep/p/1981494.html
Copyright © 2011-2022 走看看