我搜索了高低,找不到这样做的方法。我正在编写一个程序,它将在登录时运行并删除另一个目录中的目录。我们公司有一个软件应用程序,其中包含有时会损坏的目录。问题是该目录包含一些静态单词,然后附加一组随机生成的字符。因此,需要搜索静态字并删除包含它们的任何目录。这是踢我的屁股。谢谢你的帮助!
编辑:
我很抱歉没有添加我到目前为止编写的部分或全部代码。我可以删除静态目录,但不能删除动态目录。再一次,我在教自己,我确信有更好的方法来做我需要的,但我不知道。我也相对肯定我的代码很混乱等等。我会喜欢一些建设性的批评,但请不要抨击我尝试。请看下面。谢谢!
Imports System.IO
Module Module1
Public Sub Main()
'I'm wanting to see the user name output in the console
Dim user As String
user = Environment.UserName
Console.Write(user)
'new line
Console.WriteLine()
Dim path1 As String
path1 = "\appdata\local\DIRECTORY\APPLICATIONNAME.exe_Url_ny2thmvtmqmw4jiqk1yuytwfbddruu02"
Dim path2 As String
path2 = "\appdata\local\DIRECTORY\APPLICATIONNAME.exe_Url_r3joylqll52q54guz0002pxu4swqous0"
Dim fullpath As String
fullpath = "C:\Users\" & user & path1
Dim fullpath2 As String
fullpath2 = "C:\Users\" & user & path2
Dim toplevel As String
toplevel = "\appdata\local\APPLICATIONNAME\"
Dim toplevel1 As String
toplevel1 = "C:\Users" & user & toplevel
If Directory.Exists(fullpath) = True Then
Directory.Delete(fullpath, True)
ElseIf Directory.Exists(fullpath2) = True Then
Directory.Delete(fullpath2, True)
End If
'I would like to keep the window open until I work the kinks out
Console.WriteLine("Finished. You may now close this window.")
Console.ReadKey()
End Sub
End Module
答案 0 :(得分:0)
这应该做你需要的。我已经包含参数名称以使其更具可读性。如果您更喜欢更简洁的方法,可以将它们剥离出来......
' Will find all directories under C:\Root\Folder\
' (including subdirectories) with a name that starts
' with "SearchString", then delete them and their contents
System.IO.
Directory.
GetDirectories(
path:="C:\Root\Folder\",
searchPattern:="SearchString*",
searchOption:=System.IO.SearchOption.AllDirectories).
ToList().
ForEach(Sub(x) System.IO.Directory.Delete(path:=x, recursive:=True))
那就是说,这只是简单地结合了两个任务:
互联网上有很多关于这些主题的教程和示例(以及关于Stack Overflow的众多问题)。
编辑:简洁版
Imports System.IO
Directory.GetDirectories("C:\Root\Folder\", "SearchString*", SearchOption.AllDirectories).
ToList().ForEach(Sub(x) Directory.Delete(x, True))
答案 1 :(得分:0)
尝试使用类似的东西 下面的代码删除包含所列模式的每个文件夹 字符串数组。
Dim Words() As String = {"Word1","Word3","Word4",.."Wordn"}
For Each iPatternWord as String In Words
For Each iDir As System.IO.DirectoryInfo In System.IO.Directory.GetDirectories(@"C:\",iPattern)
iDir.Delete(true);//===>Delete this folder.
Loop
Loop