2015-02-24 68 views
-2

我搜索了高和低,并找不到一种方法做到这一点。我正在编写一个程序,该程序将在登录时运行并删除另一个目录中的目录。我们公司有一个软件应用程序,其中包含一个有时会损坏的目录。问题是该目录包含一些静态字词,然后附加随机生成的一组字符。因此,需要搜索静态字词并删除包含它们的任何目录。这是踢我的屁股。谢谢你的帮助!VB.NET - 搜索目录并删除它,如果它包含某些字符

编辑:

我道歉,不加入的部分或全部,我已经迄今为止编写的代码。我可以删除静态目录,但不能删除动态目录。再次,我在教自己,我确信有更好的方式来做我需要的,但我不知道他们。我也相对确定我的代码很混乱等。我会喜欢一些建设性的批评,但请不要因为尝试而激怒我。请看下面。谢谢!

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 
+1

欢迎来到StackOverflow。请发布您迄今为止尝试过的内容。 – 2015-02-24 02:27:46

+0

早上好,RS!感谢您的欢迎!你是对的 - 我应该添加我的代码。从本质上讲,我可以删除静态命名的目录,并搜索如何搜索目录名称和删除从搜索返回的任何目录代码,但我还没有发现任何这样做。当我试图写点东西的时候,我对如何执行这个动作一无所知。我很想看看如何做到这一点,并解释它背后的逻辑。我想学习。谢谢! – Deodra 2015-02-24 16:53:38

回答

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

这就是说,这简直就是将两个任务一起:

  • 查找目录
  • 删除每一个列表反过来

有关这些主题的互联网上有很多教程和例子(和堆栈溢出的许多问题)。

编辑:简洁版

Imports System.IO 

Directory.GetDirectories("C:\Root\Folder\", "SearchString*", SearchOption.AllDirectories). 
    ToList().ForEach(Sub(x) Directory.Delete(x, True)) 
+0

谢谢,基本!是的,我发现了很多关于如何删除目录的问题,但没有提及如何在目录名称中找到某些文本,然后删除返回。我感谢你的美好和尊重的答复。通常情况下,当我提问人们的问题时,我发现自己不是专家。在寻求帮助之前,我试图学会和疯狂地搜索。 – Deodra 2015-02-24 16:10:20

+0

不用担心。我编辑过,以包括简洁的版本。顺便说一句,如果您是新手,您可能希望浏览帮助中心,特别是[问](http://stackoverflow.com/help/asking)部分。当你发现你的方式时,它会为你节省一点摩擦。欢迎来到SO – Basic 2015-02-24 21:22:21

+0

再次感谢,基本!我会绝对检查该部分。非常感谢,队友! – Deodra 2015-02-25 13:52:45

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 
+0

谢谢,@Basic和@ MrAlex6204!我会尝试其中的一种或两种!你们都快乐地回答! – Deodra 2015-02-24 16:07:12