I'm making a program that allows me to control my other computer from this one by encoding and streaming the commands I enter into a console to the destination, which then decodes it and does what the command says and encodes the result and streams it back, then my side decodes it and shows the result in a console. Basically it's like command prompt I can run over a network.

One big problem I'm having is UnauthorizedAccessExceptions when it tries to access certain directories for any reason. For example, this simple code, which lists all the directories works fine but when I have it count the 1st level subdirectories in those directories (Second For Each) I get an UnauthorizedAccessException sometimes (Depends on the main directory). For example, if I use the command on C:\, I'll get an "Access to the path 'C:\Documents and Settings' is denied." error.

How can I grant my program unrestricted access or at least enough access to be able to get into directories like Documents and Settings? Also, I am unable to access Documents and Settings through explorer, I get the same error. I'm on Win7, so I'm guessing it's system protected as it's hidden and inaccessible.

Public Function Dir(ByVal Direc As String)
        Dim Results As String = String.Empty
        Dim DirC As Integer = 0
        Dim DirC2 As Integer = 0
        Try
            If Direc.Contains("DIR") Then
                Dim Text As String = Direc
                Dim Tree As String = Text.Replace("DIR ", String.Empty)
                For Each Directory In My.Computer.FileSystem.GetDirectories(Tree)
                    DirC = DirC + 1
                    For Each Dir2 In My.Computer.FileSystem.GetDirectories(Directory)
                        Try
                            DirC2 = DirC2 + 1
                        Catch ex As Exception
                            Results = ex.Message & vbNewLine
                        End Try
                    Next
                    Results = Directory & " (" & DirC2 & " Subfolder(s))" & vbNewLine
                Next
                Results &= vbNewLine
            End If
        Catch ex As IO.DirectoryNotFoundException
            If Main.IP <> String.Empty Then
                Results = "The directory: " & Tree & " does not exist." & vbNewLine
            End If
        Catch ex As Exception
            Results = ex.Message
        End Try
        Return Results
    End Function

Did you try already to run your App as Admin?
If you don't get that error while running as Admin then open the app.manifest and set the requestedExecutionLevel to:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

I already tried running the app as administrator, still got the exception.

to avoid that exception try the following code

Private Sub CreateDirTree(ByVal basePath As String)
		Dim subDir As String

		For Each singleDir As String In IO.Directory.GetFileSystemEntries(basePath)
			If New IO.FileInfo(singleDir).Attributes = IO.FileAttributes.Directory Then
				Try
					subDir = My.Computer.FileSystem.GetName(singleDir)
					Debug.WriteLine(subDir)
					CreateDirTree(My.Computer.FileSystem.CombinePath(basePath, subDir))
				Catch ex As Exception
					Debug.WriteLine(ex.GetType)
				End Try
			End If
		Next singleDir
	End Sub
echo "Hello World";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.