| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 磁盘文件编程 >> 实例分析正文
最新推荐 更多内容
ACL访问控制列表的实例
ACL访问控制列表的实例
作者:ILU    来源:乐博网整理     更新时间:2009-12-14

ACL访问控制列表的实例

分类:VB.Net

乐博网lob.cn提示:调试环境为 vs2005 +  windows2003 / windows2008 / xp / vista + .NET Framework 2.0

代码如下:

' 导入命名空间。
Imports System.Security.Principal
Imports System.Security.AccessControl
Imports System.IO

Public Class LOB

    ' 声明所需的私有变量。
    Private m_fileSecurity As FileSecurity
    Private m_accessRules As TreeNode
    Private m_auditRules As TreeNode

    Private Sub LOB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.OpenFileDialog1.FileName = ""
        Me.FileSystemRightsSelection.DataSource = System.Enum.GetNames(GetType(FileSystemRights))
        Me.AccessControlTypeSelection.DataSource = System.Enum.GetNames(GetType(AccessControlType))
    End Sub
    Private Sub BrowseFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseFile.Click
        If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.fileName.Text = Me.OpenFileDialog1.FileName
            ' 取得使用者所选取的文件的 FileSecurity 对象。
            m_fileSecurity = File.GetAccessControl(Me.fileName.Text)
            ' 刷新 TreeView 控件。
            RefreshTreeView()
        End If
    End Sub

    Private Sub AddRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddRule.Click
        If m_fileSecurity IsNot Nothing AndAlso Me.UserName.Text.Length > 0 Then
            AddToFileACE( _
              Me.UserName.Text, _
              CType(System.Enum.Parse(GetType(FileSystemRights), Me.FileSystemRightsSelection.SelectedItem.ToString), FileSystemRights), _
              CType(System.Enum.Parse(GetType(AccessControlType), Me.AccessControlTypeSelection.SelectedItem.ToString), AccessControlType))
            RefreshTreeView()
            MessageBox.Show("规则已新增", "乐博网ACL 实例", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("您尚未选取文件或指定使用者名称。")
        End If
    End Sub

    Private Sub RemoveRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveRule.Click
        If Me.ACEDetails.Nodes.Count = 0 Then
            Exit Sub
        End If

        Dim rule As FileSystemAccessRule = CType(Me.ACEDetails.SelectedNode.Tag, FileSystemAccessRule)

        If rule IsNot Nothing Then
            If rule.IsInherited = False Then
                RemoveFromFileACE(rule)
                RefreshTreeView()
                MessageBox.Show("规则已移除", "乐博网ACL 实例", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("无法移除一个继承的规则", "乐博网ACL 实例", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If
    End Sub

    Private Sub RefreshTreeView()
        Me.ACEDetails.Nodes.Clear()
        m_accessRules = New TreeNode
        m_accessRules.Text = "访问规则"
        Me.ACEDetails.Nodes.Add(m_accessRules)
        m_auditRules = New TreeNode
        m_auditRules.Text = "稽核规则"
        Me.ACEDetails.Nodes.Add(m_auditRules)
        ShowFileAccessRules(Me.fileName.Text)
        ShowFileAuditRules(Me.fileName.Text)
    End Sub

    Private Sub ShowFileAccessRules(ByVal fileName As String)
        If Me.fileName.Text.Length > 0 AndAlso File.Exists(Me.fileName.Text) Then
            Dim counter As Integer = 1
            For Each rule As FileSystemAccessRule In m_fileSecurity.GetAccessRules(True, True, GetType(NTAccount))
                Dim ruleNode As TreeNode = New TreeNode
                ruleNode.Text = "规则 " + counter.ToString
                ruleNode.Tag = rule
                Dim ruleDetailsNode As TreeNode = New TreeNode
                ruleDetailsNode.Text = "访问控制类型:" + (Microsoft.VisualBasic.IIf(rule.AccessControlType = AccessControlType.Allow, "授与", "拒绝"))
                ruleNode.Nodes.Add(ruleDetailsNode)
                ruleDetailsNode = New TreeNode
                ruleDetailsNode.Text = "文件系统权限:" + rule.FileSystemRights.ToString
                ruleNode.Nodes.Add(ruleDetailsNode)
                ruleDetailsNode = New TreeNode
                ruleDetailsNode.Text = "标识:" + rule.IdentityReference.ToString
                ruleNode.Nodes.Add(ruleDetailsNode)
                ruleDetailsNode = New TreeNode
                ruleDetailsNode.Text = "源:" + (IIf(rule.IsInherited = True, "继承的", "直接的"))
                ruleNode.Nodes.Add(ruleDetailsNode)
                Me.m_accessRules.Nodes.Add(ruleNode)
                System.Math.Min(System.Threading.Interlocked.Increment(counter), counter - 1)
            Next
        End If
    End Sub  '更多.net源码和教程,来自[乐博网 www.lob.cn]

    Private Sub ShowFileAuditRules(ByVal fileName As String)
        If Me.fileName.Text.Length > 0 AndAlso File.Exists(Me.fileName.Text) Then
            Dim counter As Integer = 1
            For Each rule As FileSystemAuditRule In m_fileSecurity.GetAuditRules(True, True, GetType(NTAccount))
                Dim ruleNode As TreeNode = New TreeNode
                ruleNode.Text = "规则 " + counter.ToString
                ruleNode.Tag = rule
                Dim ruleDetailsNode As TreeNode = New TreeNode
                ruleDetailsNode.Text = "稽核类型:" + rule.AuditFlags.ToString
                ruleNode.Nodes.Add(ruleDetailsNode)
                ruleDetailsNode = New TreeNode
                ruleDetailsNode.Text = "文件系统权限:" + rule.FileSystemRights.ToString
                ruleNode.Nodes.Add(ruleDetailsNode)
                ruleDetailsNode = New TreeNode
                ruleDetailsNode.Text = "标识:" + rule.IdentityReference.ToString
                ruleNode.Nodes.Add(ruleDetailsNode)
                ruleDetailsNode = New TreeNode
                ruleDetailsNode.Text = "源:" + (IIf(rule.IsInherited = True, "继承的", "直接的"))
                ruleNode.Nodes.Add(ruleDetailsNode)
                Me.m_auditRules.Nodes.Add(ruleNode)
                System.Math.Min(System.Threading.Interlocked.Increment(counter), counter - 1)
            Next
        End If
    End Sub

    Private Sub AddToFileACE(ByVal userName As String, ByVal rights As FileSystemRights, ByVal accessControl As AccessControlType)
        ' 建立一个代表特定账户之存取权限的 FileSystemAccessRule 对象。
        Dim ace As FileSystemAccessRule = New FileSystemAccessRule(New NTAccount(userName), rights, accessControl)

        ' 新增访问规则。
        ' 将 FileSystemAccessRule 对象新增至 FileSecurity 对象中,
        ' 此举也就表示将指定的访问控制列表权限新增至文件中。
        m_fileSecurity.AddAccessRule(ace)

        ' 将 FileSecurity 对象所描述的访问控制列表权限套用至文件。
        File.SetAccessControl(Me.fileName.Text, m_fileSecurity)
    End Sub

    Private Sub RemoveFromFileACE(ByVal rule As FileSystemAccessRule)
        ' 移除访问规则。
        ' 从 FileSecurity 对象中移除指定的 FileSystemAccessRule 对象,
        ' 此举也就表示从档案中移除指定的访问控制列表权限。
        m_fileSecurity.RemoveAccessRule(rule)

        ' 将 FileSecurity 对象所描述的访问控制列表权限套用至文件。
        File.SetAccessControl(Me.fileName.Text, m_fileSecurity)
    End Sub

    Private Sub ACEDetails_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles ACEDetails.AfterSelect

    End Sub
End Class

 

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    资源管理器打开目录的实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]