寻找vb.net程序的指导,以清理每个用户的临时文件目录

时间:2016-04-24 22:03:50

标签: vb.net delete-file temp multiple-users

我正在寻找一些关于让函数为我正在进行的程序工作的指导。

我坚持使用的功能是从远程计算机上的多个目录中删除该计算机上所有用户配置文件的所有文件。

目前我正在尝试使用目录和子目录中的所有文件输出到我的文本框,然后再添加file.delete()。

所有"项目"显示为一个字母的字符,因为它将文件路径中的每个字母分隔成一个单独的项目(可能就像我做For..Each的方式)。

我知道我犯了多个noobie错误,因为我是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\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

1 个答案:

答案 0 :(得分:1)

这就在这里:

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()

我建议添加&#39; Option Strict&#39;到代码的顶部或为项目启用它。然后编译器将更加严格&#34;要求您声明类型,然后编译器将强制您更明确地说明您正在使用的类型。它将突出显示一些错误,但它值得。