vb.net 删除多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16668148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:40:08  来源:igfitidea点击:

Delete multiple files

vb.netfiledirectorydelete-file

提问by user2404495

I'm trying to delete multiple files in same folder with vb.net, but I haven't succeed yet. Help please?

我正在尝试使用 vb.net 删除同一文件夹中的多个文件,但我还没有成功。请帮忙?

I tried

我试过

Dim FileToDelete1 As String
Dim FileToDelete2 As String
Dim FileToDelete3 As String
Dim FileToDelete4 As String
Dim FileToDelete5 As String

FileToDelete1 = Application.StartupPath & ".exe"
FileToDelete2 = Application.StartupPath & ".dll"
FileToDelete3 = Application.StartupPath & ".dll"
FileToDelete4 = Application.StartupPath & ".dll"
FileToDelete5 = Application.StartupPath & ".dll"

If System.IO.File.Exists( FileToDelete1 ) = True Then

My.Computer.FileSystem.DeleteFile( FileToDelete1 )


ElseIf System.IO.File.Exists( FileToDelete2 ) = True Then

My.Computer.FileSystem.DeleteFile( FileToDelete2 )

ElseIf System.IO.File.Exists( FileToDelete3 ) = True Then

My.Computer.FileSystem.DeleteFile( FileToDelete3 )

ElseIf System.IO.File.Exists( FileToDelete4 ) = True Then

My.Computer.FileSystem.DeleteFile( FileToDelete4 )

ElseIf System.IO.File.Exists( FileToDelete5 ) = True Then

My.Computer.FileSystem.DeleteFile( FileToDelete5 )

End If

回答by Sérgio Ribeiro

Several problems here.

这里有几个问题。

First, File.Existsreturns a Boolean value. The "=True"is unnecessary because you're basically asking if True=True. Fortunately, it is.

首先,File.Exists返回一个布尔值。的"=True",因为你基本上是问,如果是不必要的True=True。幸运的是,确实如此。

Second, file existence or not it's not the only way to fail. For instance, if the file is in use, you'll get an exception. You should handle it.

其次,文件存在与否并不是失败的唯一途径。例如,如果文件正在使用中,您将收到异常。你应该处理它。

Third, what if you need to delete a thousand files? Would you create a Stringfor each one of them? There are better options, for instance, the GetFilesmethod which will return a ReadOnly List of Strings, each one representing one file. I don't know your needs, but to catch the files you mention, the following call can be made:

三、如果需要删除一千个文件怎么办?你会String为他们每个人创建一个吗?有更好的选择,例如,GetFiles将返回 a的方法ReadOnly List of Strings,每个方法代表一个文件。我不知道您的需求,但要捕获您提到的文件,可以进行以下调用:

FileIO.FileSystem.GetFiles(Application.StartupPath, FileIO.SearchOption.SearchTopLevelOnly, {"?.exe", "?.dll"})

FileIO.FileSystem.GetFiles(Application.StartupPath, FileIO.SearchOption.SearchTopLevelOnly, {"?.exe", "?.dll"})

It will get every EXE and DLL file if it's name consists in only one character.

如果名称仅包含一个字符,它将获取每个 EXE 和 DLL 文件。

Finally, notice that if the first condition is met, no other will be evaluated, hence no other file will be deleted. With that implementation you'll need to run the program 5 times in order to delete every file. GetFiles method solves this as well.

最后,请注意,如果满足第一个条件,则不会评估其他文件,因此不会删除其他文件。使用该实现,您需要运行该程序 5 次才能删除每个文件。GetFiles 方法也解决了这个问题。

Additionally, consider importing namespaces so you don't need to prefix them in every method call.

此外,请考虑导入命名空间,这样您就无需在每个方法调用中为其添加前缀。

回答by Div Tiwari

Looks like you want to do some thing like this

看起来你想做这样的事情

Dim fileNames() as string={"1","2","3"}
Dim fileTypes() as string={"exe","dll"} 

directory.SetCurrentDirectory(Application.StartupPath)
  For each fileName as string in fileNames
    For each fileType as string in fileTypes
        if My.Computer.FileSystem .FileExists (fileName &"."& fileType) then
          try
            My.Computer.FileSystem.DeleteFile( fileName &"."& fileType )
          catch ex As Exception
            '**** processings related with exception.
          end try
        endif
        'Dim files() As String = Directory.GetFiles(dirPath, fileName &"." & fileType, SearchOption.AllDirectories)
        'For Each FileToDelete as string in files
        '   My.Computer.FileSystem.DeleteFile( FileToDelete )
        'Next
    Next
  Next