[Fork] [Fork] [Fork] https://stackoverflow.com/questions/65443704/how-to-make-my-vb-net-code-dynamic-instead-of-static

VbNet · Console

/Program.vb

Imports System
Imports System.Linq
Imports System.Collections
Imports System.Collections.Generic
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Security.AccessControl

Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports Newtonsoft.Json.Converters
Imports Newtonsoft.Json.Serialization

Imports NUnit.Framework

	' https://stackoverflow.com/questions/65443704/how-to-make-my-vb-net-code-dynamic-instead-of-static

Public Module DirectoryExtensions
    Function GetFileSystemAccessRules(d As DirectoryInfo) As IEnumerable(Of FileSystemAccessRule)
        Dim ds As DirectorySecurity = d.GetAccessControl()
        Dim arrRules As AuthorizationRuleCollection = ds.GetAccessRules(True, True, GetType(Security.Principal.NTAccount))
		Return arrRules.Cast(Of FileSystemAccessRule)()
    End Function
	
	Public Function SerializeFileAccessRules(ByVal curDirectory As String, Optional ByVal formatting As Formatting = Formatting.Indented)
        Dim di As DirectoryInfo = New DirectoryInfo(curDirectory)

        Dim directoryQuery = RecursiveEnumerableExtensions.Traverse(di, Function(d) d.GetDirectories())

        Dim list = directoryQuery.Select(
            Function(d) New With {
                .directory = d.FullName,
				.permissions = GetFileSystemAccessRules(d).Select(
					Function(a) New With { 
						.IdentityReference = a.IdentityReference.ToString(),
						.AccessControlType = a.AccessControlType.ToString(),
						.FileSystemRights = a.FileSystemRights.ToString(),
						.IsInherited = a.IsInherited.ToString()
					}
				)
			}
        )
		Return JsonConvert.SerializeObject(list, formatting)	
	End Function
End Module

Public Module RecursiveEnumerableExtensions
	' Translated to vb.net from this answer https://stackoverflow.com/a/60997251/3744182
	' To https://stackoverflow.com/questions/60994574/how-to-extract-all-values-for-all-jsonproperty-objects-with-a-specified-name-fro
	' which was rewritten from the answer by Eric Lippert https://stackoverflow.com/users/88656/eric-lippert
	' to "Efficient graph traversal with LINQ - eliminating recursion" https://stackoverflow.com/questions/10253161/efficient-graph-traversal-with-linq-eliminating-recursion
    Iterator Function Traverse(Of T)(ByVal root As T, ByVal children As Func(Of T, IEnumerable(Of T)), ByVal Optional includeSelf As Boolean = True) As IEnumerable(Of T)
        If includeSelf Then Yield root
        Dim stack = New Stack(Of IEnumerator(Of T))()

        Try
            stack.Push(children(root).GetEnumerator())
            While stack.Count <> 0
                Dim enumerator = stack.Peek()
                If Not enumerator.MoveNext() Then
                    stack.Pop()
                    enumerator.Dispose()
                Else
                    Yield enumerator.Current
                    stack.Push(children(enumerator.Current).GetEnumerator())
                End If
            End While
        Finally
            For Each enumerator In stack
                enumerator.Dispose()
            Next
        End Try
    End Function
End Module
					
Public Module TestClass
    Friend Sub Test()
        'Dim str_rootdirectory As String = Directory.GetCurrentDirectory() ' "C:\TEMP"
		Test(Directory.GetCurrentDirectory())
	End Sub

	Friend Sub Test(ByVal curDirectory As String)
		Dim json As String = DirectoryExtensions.SerializeFileAccessRules(curDirectory)
        File.WriteAllText("ABCD.json", json)
    End Sub
End Module

Public Module Module1	
	Public Sub Main()
		Console.WriteLine("Environment version: " & Environment.Version.ToString())
		Console.WriteLine("Json.NET version: " & GetType(JsonSerializer).Assembly.FullName)
        Console.WriteLine("")

		Try

			TestClass.Test()
			TestClass.Test("C:\TEMP")

		Catch ex as Exception
			Console.WriteLine("Unhandled exception: ")
			Console.WriteLine(ex)
			Throw
		End Try
	End Sub	
End Module
						
' It's not clear where your problem is exactly because you don't share a [mcve], e.g. you don't show how `arrRules` is determined.  But is this what you want?  https://dotnetfiddle.net/zTd3tW  The code here enumerates through a directory hierarchy and, for each `DirectoryInfo`, enumerates through its `FileSystemAccessRule` objects and projects the result to a dictionary.  note that the code won't run on https://dotnetfiddle.net/ because of security permission issues.							
							
;