检查目录[VB.NET]中的隐藏文件/文件夹

检查目录[VB.NET]中的隐藏文件/文件夹,vb.net,Vb.net,我想检查目录/USB驱动器中是否有隐藏的文件/文件夹。但我想排除系统文件,如thumbs.db、*.ini和系统卷信息。如何做到这一点 我试过了 Dim exclude = {"thumbs.db", "*.ini", "System Volume Information"} If (myDirectory.Attributes.ToString.Contains("Hidden").Except(exclude)) Then ''do something else ''do someth

我想检查目录/USB驱动器中是否有隐藏的文件/文件夹。但我想排除系统文件,如thumbs.db、*.ini和系统卷信息。如何做到这一点

我试过了

Dim exclude = {"thumbs.db", "*.ini", "System Volume Information"}
If (myDirectory.Attributes.ToString.Contains("Hidden").Except(exclude)) Then

''do something

else

''do something else

End If

但是我得到的错误是,
除了
不是
boolean
的成员之外,您需要这样一个递归过程:

    Dim fiArr As FileInfo() = myDirectory.GetFiles()
    Dim fri As FileInfo
    For Each fri In fiArr
        if (fri.Attributes.ToString.Contains("Hidden") and not fri.Name.Contains("thumbs.db")  and not fri.Name.Contains(".ini") and not fri.Name.Contains("System Volume Information"))
            ''do something
        else
            ''do something else
        End If
    Next fri

Dim dirArr As DirectoryInfo() = myDirectory.GetDirectories()
    Dim dinfo As DirectoryInfo
    For Each dinfo In dirArr
        if (dinfo.Attributes.ToString.Contains("Hidden") and not dinfo.Name.Contains("thumbs.db")  and not dinfo.Name.Contains(".ini") and not dinfo.Name.Contains("System Volume Information"))
            ''do something
        else
            ''do something else
        End If
    Next dinfo
Module StartupModule

    Sub Main()
        Dim di As New IO.DirectoryInfo("C:\Windows\Fonts")
        Dim paths As List(Of String) = FindHidden(di)

        For Each p In paths
            Console.WriteLine(p)
        Next

        Console.ReadLine()
    End Sub

    Private Function FindHidden(di As IO.DirectoryInfo) As List(Of String)
        Static paths As New List(Of String)

        If (di.Attributes And IO.FileAttributes.System) <> IO.FileAttributes.System Then
            If ((di.Attributes And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden) Then
                paths.Add(di.FullName)
            End If
        End If

        For Each f In di.GetFiles
            If ((f.Attributes And IO.FileAttributes.System) <> IO.FileAttributes.System) Then
                If ((f.Attributes And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden) Then
                    paths.Add(f.FullName)
                End If
            End If
        Next

        For Each d In di.GetDirectories
            FindHidden(d)
        Next

        Return paths
    End Function

End Module
模块启动模块
副标题()
将di设置为新IO.DirectoryInfo(“C:\Windows\Fonts”)
作为列表的Dim路径(字符串)=FindHidden(di)
对于路径中的每个p
控制台写入线(p)
下一个
Console.ReadLine()
端接头
私有函数FindHidden(di作为IO.DirectoryInfo)作为列表(字符串)
静态路径作为新列表(字符串)
如果(di.Attributes和IO.FileAttributes.System)为IO.FileAttributes.System,则
如果((di.Attributes和IO.FileAttributes.Hidden)=IO.FileAttributes.Hidden),则
path.Add(di.FullName)
如果结束
如果结束
对于di.GetFiles中的每个f
如果((f.Attributes和IO.FileAttributes.System)IO.FileAttributes.System),则
如果((f.Attributes和IO.FileAttributes.Hidden)=IO.FileAttributes.Hidden),则
路径。添加(f.FullName)
如果结束
如果结束
下一个
对于di.getDirectory中的每个d
芬迪登(d)
下一个
返回路径
端函数
端模块
您必须稍微处理一下,因为对于只读文件会引发异常(我正在工作,所以没有太多时间)


我想你明白了。

@我明白了,暂停。你知道怎么做吗?谢谢你的回答。代码是完美的。没有错误。但在查找隐藏文件夹时返回false。即使有隐藏的文件夹。更新的解决方案,复制文件逻辑到文件夹。这是伟大的!但是有一个小问题。它跳过了一行代码。
如果(dinfo.Attributes.ToString.Contains(“隐藏”)而不是dinfo.Name.Contains(“thumbs.db”)而不是dinfo.Name.Contains(“ini”)而不是dinfo.Name.Contains(“系统卷信息”)),则DriveClean=False unhide.ForeColor=Color.FromArgb(232,17,35)Else unhide.ForeColor=Color.FromArgb(0,163,2)End If Next dinfo
当DriveClean为false时,它不会将颜色更改为红色。问题中的“DriveClean”在哪里?我的解决办法是解决你的问题。要添加附加条件,只需在if语句中添加'and DriveClean=False'。谢谢@shadow的回答。我会让你知道它是否有效