PShell_enthusiast93 PShell_enthusiast93 - 7 months ago 9
Vb.net Question

Looking for guidance for vb.net program to cleanup temp file directories for every user

I am looking for some guidance on getting a function working for a program I am working on.

The function I am stuck on is meant to remove all files from multiple directories on a remote computer for all user profiles on that machine.

At the moment I am trying to get output to my textbox with all of the files in the directories and subdirectories before I added the file.delete().

All of the "items" are displaying as one letter characters as it is separating each letter in the filepath into a separate item (probably the way I did the

For..Each
).

I know I am making multiple noobie mistakes as I am very new to VB.net and am still very much in the learning stage but I am looking for some guidance.

I have been searching and pulling pieces together to get it working but have hit a wall, any help is appreciated!

Current Code:

Private Sub Deltemp_Click(sender As Object, e As EventArgs) Handles Deltemp.Click
If Compname2.Text = "" Then
MessageBox.Show("You didn't enter anything in the Computername field, please enter something then try again.")
GoTo statementend
End If
Try
If My.Computer.Network.Ping(Compname2.Text, 1000) Then
PSoutput.Text = ""
Else
PSoutput.Text &= Compname2.Text + " is not connected to our network currently."
GoTo statementend
End If
PSoutput.Text = ""
Dim userpath As String = "\\" + Compname2.Text + "\c$\users\"
Dim tempu(7) As String
tempu(0) = "\AppData\Local\Temp\"
tempu(1) = "\AppData\Local\Microsoft\Windows\Temporary Internet Files\"
tempu(2) = "\AppData\Local\Microsoft\Credentials"
tempu(3) = "\AppData\Local\Temporary Internet Files\"
tempu(4) = "\AppData\Roaming\Microsoft\Credentials"
tempu(5) = "\AppData\Roaming\SUN"
tempu(6) = "\AppData\Local\Apps\2.0"
Dim fsd As Object
fsd = FileIO.FileSystem.GetDirectories(userpath)
For Each user In fsd
Try
Dim filepaths = FileIO.SpecialDirectories.AllUsersApplicationData()
If user Like "*Default*" Or user Like "*.NET*" Or user Like "*All Users" Or user Like "*Public" Then
PSoutput.Text &= ""
Else
Dim Fullpath = user + tempu(7)
For Each item In Fullpath
FileIO.FileSystem.GetFiles(Fullpath)
PSoutput.Text &= item.ToString + " was found" & Environment.NewLine
Next
End If
Catch ex As Exception
PSoutput.Text &= "A file was skipped for being in use or an exception occured" & Environment.NewLine
End Try
Next
Catch
MessageBox.Show("The machine/ip entered doesn't exist on our network or is an invalid entry")
End Try
statementend:
End Sub

Answer

It's this right here:

Dim Fullpath = user + tempu(7)
    For Each item In Fullpath '<<<<<<<<<

Fullpath appears to be a string, so when you do For-Each you're iterating through the characters in that string.

I think what you want is to create a new DirectoryInfo, get the files in that directory, and iterate through them.

dim directory = new System.io.DirectoryInfo(Fullpath)
For Each file in directory.GetFiles()

I recommend adding 'Option Strict' to the top of your code or enabling it for the project. Then the compiler will be more "strict" in requiring you to declare types, and then the compiler will force you to be more explicit about what types you're working with. It will highlight some errors to fix but it's worth it.