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

问题描述:

我正在寻找一些关于获取我正在处理的程序的函数的指导。寻找vb.net程序的指导,以清理每个用户的临时文件目录

我被卡住的功能是为了从该计算机上的所有用户配置文件的远程计算机上的多个目录中删除所有文件。

目前我正在尝试在我添加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 

正是这种在这里:

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'或为项目启用它。然后编译器会更加“严格”地要求你声明类型,然后编译器会强制你更清楚地知道你正在使用哪种类型。它会突出一些错误来解决,但它是值得的。

+0

我知道它必须是我很想念的东西,谢谢你斯科特! - 额外的问题,如果你可以,我注意到,当我这样运行时,我只得到所有目录中的文件,我也没有看到一个get目录,当我点源目录时,无论如何要获取里面的所有文件包括所有子目录?也感谢你的'选择严格'的提示,我会得到这个实施:) –

+1

不,你需要递归获取每个目录中的目录并获取每个目录中的文件。所以你需要一个更多的方法 - 你传递一个目录,然后获得该目录中的所有子目录并且调用它自己。这样一直持续到所有的子目录,直到没有更多。 –

+1

这里是[示例](https://support.microsoft.com/en-us/kb/306666)和[另一个](http://www.coderslexicon.com/playing-with-recursive-directory-diving -in-VB-净/)。 –