Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27159

VS 2010 How to compare records in a function

$
0
0
Ok, i'm currently in the process of writing an application for work purposes... its basically an ACL security scan on certain folders.

here is a picture of what i'm doing.

Attachment 97399

You click search for folders, and it puts the entire folder structure, and subfolder structure into a treeview.
Then click Expand treeview, it expands it, and then write all complete folder locations to the richtextbox to the right.

Then you click get results, and it checks every single line, and posts the results from each line. This part works fine.

What i'm trying to do is.... compare all of the folders and subfolders, and if all the ACL's match (if there the same) only show the first one, and not all the subfolders assosiated with it. How can this be done?

Below is my full code i'm using to complete this task.

Function ListDirectoryACLs is the function that pulls the ACL security.



Code:

Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Security.AccessControl

Public Class folderACLsearch


    Dim computername As String

    'creating threads
    Dim t1 As Thread
    Dim t2 As Thread
    Dim t3 As Thread
    Dim t4 As Thread
    Dim t5 As Thread
    Dim t6 As Thread
    Dim t7 As Thread

    '----------------------------------------Treeview1 Population-------------------------------------------------------------------------

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim s As String
        Dim sp() As String
        s = TextBox1.Text
        sp = s.Split("*")
        For Each substring In sp
            Dim rootnode As New TreeNode(substring)
            TreeView1.Nodes.Add(rootnode)
            FillChildNodes(rootnode)
            TreeView1.Nodes(0).Expand()
        Next
    End Sub

    Private Sub FillChildNodes(node As TreeNode)
        Dim dirs As New DirectoryInfo(node.FullPath)
        For Each dir As DirectoryInfo In dirs.GetDirectories()
            Dim newnode As New TreeNode(dir.Name)
            node.Nodes.Add(newnode)
            newnode.Nodes.Add("*")
        Next
    End Sub

    Private Sub treeView1_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
        If e.Node.Nodes(0).Text = "*" Then
            e.Node.Nodes.Clear()
            FillChildNodes(e.Node)
        End If
    End Sub

    '----------------------------------------Listbox1 Population-------------------------------------------------------------------------

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        Try
            ToolStripProgressBar1.Visible = True
            ToolStripProgressBar1.Style = ProgressBarStyle.Marquee
            ToolStripProgressBar1.MarqueeAnimationSpeed = 1
            ToolStripStatusLabel1.Text = "Currently expanding treeview... this might take a minute!"
        Catch ex As Exception
        Finally

            t1 = New Thread(AddressOf expand)
            t1.Priority = ThreadPriority.Lowest
            t1.Start()

            t2 = New Thread(AddressOf expand)
            t2.Priority = ThreadPriority.Lowest
            t2.Start()

            t3 = New Thread(AddressOf expand)
            t3.Priority = ThreadPriority.Lowest
            t3.Start()

            t4 = New Thread(AddressOf expand)
            t4.Priority = ThreadPriority.Lowest
            t4.Start()

            t5 = New Thread(AddressOf expand)
            t5.Priority = ThreadPriority.Lowest
            t5.Start()
        End Try

    End Sub

    Public Sub expand()

        Try
            TreeView1.ExpandAll()

        Catch ex As Exception
            'MessageBox.Show(ex.ToString)
        Finally

            'ListBox1.Items.AddRange(Split(RichTextBox1.Text, vbLf))

            t2.Abort()
            t3.Abort()
            t4.Abort()
            t5.Abort()

            ToolStripStatusLabel1.Text = "Done! Waiting for another command..."
        End Try



        ToolStripStatusLabel1.Text = "Populating full paths of all treenodes..."

        t6 = New Thread(AddressOf dosome)
        t6.Start()


    End Sub

    Public Sub dosome()
        'create buffer for storing string data
        Dim buffer As New System.Text.StringBuilder
        'loop through each of the treeview's root nodes
        For Each rootNode As TreeNode In TreeView1.Nodes
            'call recursive function
            BuildTreeString(rootNode, buffer)
        Next
        'write data to file
        RichTextBox1.Text = buffer.ToString

        'IO.File.WriteAllText("C:\treeTest.txt", buffer.ToString)
        Me.RichTextBox1.Lines = Me.RichTextBox1.Text.Split(New Char() {ControlChars.Lf}, _
                                                  StringSplitOptions.RemoveEmptyEntries)

        ListBox1.Items.AddRange(Split(RichTextBox1.Text, vbLf))

        ToolStripProgressBar1.Style = ProgressBarStyle.Continuous
        ToolStripStatusLabel1.Text = "Done! Waiting for another command..."
    End Sub

    Public Function ListDirectoryACLs(ByVal directory As String) As String

        Dim oDirACLs As New Security.AccessControl.DirectorySecurity(directory, Security.AccessControl.AccessControlSections.Access)
        Dim sbAccess As New System.Text.StringBuilder()

        sbAccess.AppendLine(New String("="c, 100))
        sbAccess.AppendFormat("File Path: {0}", computername).AppendLine()
        sbAccess.AppendLine(New String("="c, 100))
        sbAccess.AppendLine(Environment.NewLine)

        For Each oAccessRule As System.Security.AccessControl.FileSystemAccessRule In oDirACLs.GetAccessRules(True, True, GetType(System.Security.Principal.NTAccount))

            Dim ACL_Type As String = ""
            ' Determine if this ACL is an Allow or Deny type. 
            If oAccessRule.AccessControlType.Equals(Security.AccessControl.AccessControlType.Deny) Then
                ACL_Type = "Deny"
            Else
                ACL_Type = "Allow"
            End If

            ' Enumerate all the individual ACLs which make up the main ACL. 
            Dim ACL_PermissionFC As String = "Null"
            If (oAccessRule.FileSystemRights And FileSystemRights.FullControl) = FileSystemRights.FullControl Then ACL_PermissionFC = ACL_Type

            Dim ACL_PermissionM As String = "Null"
            If (oAccessRule.FileSystemRights And FileSystemRights.Modify) = FileSystemRights.Modify Then ACL_PermissionM = ACL_Type

            Dim ACL_PermissionRE As String = "Null"
            If (oAccessRule.FileSystemRights And FileSystemRights.ReadAndExecute) = FileSystemRights.ReadAndExecute Then ACL_PermissionRE = ACL_Type

            Dim ACL_PermissionLD As String = "Null"
            If (oAccessRule.FileSystemRights And FileSystemRights.ListDirectory) = FileSystemRights.ListDirectory Then ACL_PermissionLD = ACL_Type

            Dim ACL_PermissionR As String = "Null"
            If (oAccessRule.FileSystemRights And FileSystemRights.Read) = FileSystemRights.Read Then ACL_PermissionR = ACL_Type

            Dim ACL_PermissionW As String = "Null"
            If (oAccessRule.FileSystemRights And FileSystemRights.Write) = FileSystemRights.Write Then ACL_PermissionW = ACL_Type

            Dim ACL_PermissionS As String = "Null"
            If (oAccessRule.FileSystemRights And FileSystemRights.Traverse) = FileSystemRights.Traverse Then ACL_PermissionW = ACL_Type

            sbAccess.AppendFormat("Account:    {0}", oAccessRule.IdentityReference.Value).AppendLine()
            sbAccess.AppendFormat("Type:        {0}", oAccessRule.AccessControlType).AppendLine()
            sbAccess.AppendFormat("Rights:      {0}", oAccessRule.FileSystemRights).AppendLine()
            sbAccess.AppendFormat("Inherited:  {0}", oAccessRule.IsInherited).AppendLine()
            sbAccess.AppendFormat("Inheritance: {0}", oAccessRule.InheritanceFlags).AppendLine()
            sbAccess.AppendFormat("Propagation: {0}", oAccessRule.PropagationFlags).AppendLine()
            sbAccess.AppendFormat("Full Control: {0}", ACL_PermissionFC).AppendLine()
            sbAccess.AppendFormat("Modify Security: {0}", ACL_PermissionM).AppendLine()
            sbAccess.AppendFormat("Read & Execute: {0}", ACL_PermissionRE).AppendLine()
            sbAccess.AppendFormat("List Folder Contents: {0}", ACL_PermissionLD).AppendLine()
            sbAccess.AppendFormat("Read Security: {0}", ACL_PermissionR).AppendLine()
            sbAccess.AppendFormat("Write Security: {0}", ACL_PermissionW).AppendLine()

            sbAccess.AppendLine(New String("-"c, 25))

        Next

        Return sbAccess.ToString()
    End Function

    Private Sub BuildTreeString(ByVal rootNode As TreeNode, ByRef buffer As System.Text.StringBuilder)
        buffer.Append(rootNode.FullPath.ToString)
        buffer.Append(Environment.NewLine)
        For Each childNode As TreeNode In rootNode.Nodes
            BuildTreeString(childNode, buffer)
        Next
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

        ToolStripProgressBar1.Style = ProgressBarStyle.Marquee
        ToolStripStatusLabel1.Text = "Currently writing logs to file...this could take a minute!"
        t7 = New Thread(AddressOf logs)
        t7.Start()

    End Sub
    Public Sub logs()

        On Error GoTo ErrorHandler

        For i As Integer = 0 To ListBox1.Items.Count
            computername = ListBox1.Items([i].ToString())
            folderACLsearchresults.RichTextBox1.AppendText(ListDirectoryACLs(computername))
        Next

ErrorHandler:

        If folderACLsearchresults.ShowInTaskbar = True Then
            ToolStripProgressBar1.Visible = False
            ToolStripStatusLabel1.Text = "Done! Waiting for another command..."

        End If

        Resume Next

        'use showdialog to show a form in a thread :) FINALLY FOUND THIS ****.
        folderACLsearchresults.ShowDialog()

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
End Class

Any help will be GREATLY appreciated.
Attached Images
 

Viewing all articles
Browse latest Browse all 27159

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>