删除除3个文件VB.NET以外的所有文件 [英] Delete all files except 3 files VB.NET
本文介绍了删除除3个文件VB.NET以外的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好!
我有这个代码:
Hello!
I have this code:
Private Sub DeleteOldFiles(
ByVal AsDirectoryName As String,
ByVal AbDeleteSubDirectories As Boolean,
ByVal AbDeleteFolders As Boolean)
If AbDeleteSubDirectories Then
Dim m_sSubdirectoryName As String
For Each m_sSubdirectoryName In
System.IO.Directory.GetDirectories(AsDirectoryName)
DeleteOldFiles _
(m_sSubdirectoryName,
AbDeleteSubDirectories,
AbDeleteFolders)
If AbDeleteFolders Then
System.IO.Directory.Delete(m_sSubdirectoryName)
End If
Next
End If
Dim m_sFileName As String
For Each m_sFileName In
System.IO.Directory.GetFiles(AsDirectoryName)
If m_sFileName <> "exceptme.txt" Then
System.IO.File.Delete(m_sFileName)
End If
Next
和DeleteOldFiles(./,True,True)在按钮控件上。
但是,此代码仅适用于一个文件。
i想要除了更多文件。
我该怎么做?
我尝试过:
i试图除了更多的文件,但我不知道如何。
and DeleteOldFiles("./", True, True) on a button control.
but, this code works only with one file.
i want to except more files.
how can I do this?
What I have tried:
i tried to except more files, but i don't know how.
推荐答案
你可以把你想要排除的所有文件名放在当前在数组中操作,然后使用Array.Exists()
进行检查。
您的代码应该看起来类似于 -
You can put all the file names those you want to be excluded from the current operation in an array and then make use ofArray.Exists()
for checking.
You code should look something like -
For Each m_sFileName In
System.IO.Directory.GetFiles(AsDirectoryName)
Dim FilesToExclude As String() = {"exceptme.txt", "exceptmetoo.txt", "igonemetoo.txt"}
If Not Array.Exists(FilesToExclude, Function(element) element = m_sFileName) Then
System.IO.File.Delete(m_sFileName)
End If
Next
希望,它有帮助:)
Hope, it helps :)
我后来修复如下:
i fixed later like this:
Private Sub DeleteOldFiles(
ByVal AsDirectoryName As String,
ByVal AbDeleteSubDirectories As Boolean,
ByVal AbDeleteFolders As Boolean,
ByVal exemptFileNames As IEnumerable(Of String))
If AbDeleteSubDirectories Then
Dim m_sSubdirectoryName As String
For Each m_sSubdirectoryName In
System.IO.Directory.GetDirectories(AsDirectoryName)
DeleteOldFiles _
(m_sSubdirectoryName,
AbDeleteSubDirectories,
AbDeleteFolders,
exemptFileNames)
If AbDeleteFolders Then
System.IO.Directory.Delete(m_sSubdirectoryName)
End If
Next
End If
Dim m_sFileName As String
For Each m_sFileName In
System.IO.Directory.GetFiles(AsDirectoryName)
If exemptFileNames.Contains(m_sFileName) Then
MsgBox("Skipped " & m_sFileName)
Else
System.IO.File.Delete(m_sFileName)
End If
Next
End Sub
和用法:
and usage:
Dim exemptFilenames As New List(Of String)()
exemptFilenames.Add("./exceptme.txt")
exemptFilenames.Add("./exceptmetoo.txt")
exemptFilenames.Add("./ignoremetoo.txt")
DeleteOldFiles("./", True, True, exemptFilenames)
这篇关于删除除3个文件VB.NET以外的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文