删除除3个文件VB.NET以外的所有文件

编程入门 行业动态 更新时间:2024-10-24 06:25:59
本文介绍了删除除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 of Array.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以外的所有文件

本文发布于:2023-10-31 12:54:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   VB   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!