寻找 vb.net 程序的指导来为每个用户清理临时文件目录
Looking for guidance for vb.net program to cleanup temp file directories for every user
我正在寻找一些指导,让我正在处理的程序运行一个函数。
我坚持的功能是从远程计算机上的多个目录中删除该计算机上所有用户配置文件的所有文件。
在我添加 file.delete() 之前,我正在尝试将目录和子目录中的所有文件输出到我的文本框。
所有 "items" 都显示为一个字母字符,因为它将文件路径中的每个字母分隔成一个单独的项目(可能是我做 For..Each
的方式)。
我知道我犯了很多新手错误,因为我是 VB.net 的新手,而且仍处于学习阶段,但我正在寻找一些指导。
我一直在搜索和拼凑各个部分以使其正常工作,但遇到了困难,感谢任何帮助!
当前代码:
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.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
就是这里:
Dim Fullpath = user + tempu(7)
For Each item In Fullpath '<<<<<<<<<
Fullpath
似乎是一个字符串,所以当您执行 For-Each
时,您正在遍历该字符串中的字符。
我想你想要的是创建一个新的 DirectoryInfo
,获取该目录中的文件,然后遍历它们。
dim directory = new System.io.DirectoryInfo(Fullpath)
For Each file in directory.GetFiles()
我建议将 'Option Strict' 添加到代码的顶部或为项目启用它。然后编译器将更多地 "strict" 要求您声明类型,然后编译器将强制您更明确地说明您正在使用的类型。它会突出显示一些要修复的错误,但这是值得的。
我正在寻找一些指导,让我正在处理的程序运行一个函数。
我坚持的功能是从远程计算机上的多个目录中删除该计算机上所有用户配置文件的所有文件。
在我添加 file.delete() 之前,我正在尝试将目录和子目录中的所有文件输出到我的文本框。
所有 "items" 都显示为一个字母字符,因为它将文件路径中的每个字母分隔成一个单独的项目(可能是我做 For..Each
的方式)。
我知道我犯了很多新手错误,因为我是 VB.net 的新手,而且仍处于学习阶段,但我正在寻找一些指导。
我一直在搜索和拼凑各个部分以使其正常工作,但遇到了困难,感谢任何帮助!
当前代码:
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.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
就是这里:
Dim Fullpath = user + tempu(7)
For Each item In Fullpath '<<<<<<<<<
Fullpath
似乎是一个字符串,所以当您执行 For-Each
时,您正在遍历该字符串中的字符。
我想你想要的是创建一个新的 DirectoryInfo
,获取该目录中的文件,然后遍历它们。
dim directory = new System.io.DirectoryInfo(Fullpath)
For Each file in directory.GetFiles()
我建议将 'Option Strict' 添加到代码的顶部或为项目启用它。然后编译器将更多地 "strict" 要求您声明类型,然后编译器将强制您更明确地说明您正在使用的类型。它会突出显示一些要修复的错误,但这是值得的。