如何检查C驱动器VB.NET中是否存在文件/文件夹

如何检查C驱动器VB.NET中是否存在文件/文件夹,vb.net,Vb.net,我正在尝试创建一个简单的反作弊程序,它可以检测所有C驱动器中的外部作弊,但我不知道如何检查所有C驱动器中是否存在确定的文件,有什么帮助吗?以下是我改编的代码。在本例中,如果在C驱动器的任何位置找到文件bit32.txt,则FileExists()函数返回True。并非所有文件和目录都可以访问。在这种情况下,函数将显示这些文件和目录的路径。在这些情况下,您可能需要调整代码以执行任何您想要的操作 Imports System.IO Module Module1 Private Sub M

我正在尝试创建一个简单的反作弊程序,它可以检测所有C驱动器中的外部作弊,但我不知道如何检查所有C驱动器中是否存在确定的文件,有什么帮助吗?

以下是我改编的代码。在本例中,如果在
C
驱动器的任何位置找到文件
bit32.txt
,则
FileExists()
函数返回
True
。并非所有文件和目录都可以访问。在这种情况下,函数将显示这些文件和目录的路径。在这些情况下,您可能需要调整代码以执行任何您想要的操作

Imports System.IO

Module Module1

    Private Sub Main(ByVal args() As String)
        Dim exists As Boolean = FileExists(New DriveInfo("C").RootDirectory, "bit32.txt")
    End Sub

    Private Function FileExists(ByVal root As DirectoryInfo, ByVal filename As String) As Boolean
        Dim files() As FileInfo = Nothing
        Dim subDirs() As DirectoryInfo = Nothing
        ' First, process all the files directly under this folder
        Try
            files = root.GetFiles("*.*")
        Catch e As Exception
            ' This code just writes out the message and continues to recurse.
            ' You may decide to do something different here. For example, you
            ' can try to elevate your privileges and access the file again.
            Console.WriteLine(e.Message)
        End Try

        If (Not (files) Is Nothing) Then
            For Each fi As FileInfo In files
                ' In this example, we only access the existing FileInfo object. If we
                ' want to open, delete or modify the file, then
                ' a try-catch block is required here to handle the case
                ' where the file has been deleted since the call to TraverseTree().
                'Console.WriteLine(fi.FullName);
                If (fi.Name = filename) Then
                    Return True
                End If

            Next
        End If

        Try
            ' Now find all the subdirectories under this directory.
            subDirs = root.GetDirectories
            For Each dirInfo As DirectoryInfo In subDirs
                ' Resursive call for each subdirectory.
                If FileExists(dirInfo, filename) Then
                    Return True
                End If

            Next
        Catch e As Exception
            ' This code just writes out the message and continues to recurse.
            ' You may decide to do something different here. For example, you
            ' can try to elevate your privileges and access the file again.
            Console.WriteLine(e.Message)
        End Try

        Return False
    End Function
End Module

您可以在
DirectoryInfo
上拥有一个扩展名,该扩展名以非递归方式遍历根目录以查找文件

<Extension()>
Public Iterator Function GetFilesDepthFirst(ByVal root As DirectoryInfo, ByVal Optional dirPattern As String = "*", ByVal Optional filePattern As String = "*.*") As IEnumerable(Of FileInfo)
    Dim stack = New Stack(Of DirectoryInfo)()
    stack.Push(root)

    While stack.Count > 0
        Dim current = stack.Pop()
        Dim files = Enumerable.Empty(Of FileInfo)()
        Dim dirs = Enumerable.Empty(Of DirectoryInfo)()

        Try
            dirs = current.EnumerateDirectories(searchPattern:=dirPattern)
            files = current.EnumerateFiles(searchPattern:=filePattern)
        Catch ex1 As UnauthorizedAccessException
        Catch ex2 As PathTooLongException
        End Try

        For Each file As FileInfo In files
            Yield file
        Next

        For Each dir As DirectoryInfo In dirs
            stack.Push(dir)
        Next
    End While
End Function

而且,您在起始目录中的位置越具体,它运行的速度就越快。通常从C:\的根目录进行搜索是一个非常缓慢和糟糕的主意。

您所说的“所有C驱动器”是什么意思?你是说C驱动器上的任何地方吗?这里有一些递归文件搜索的例子。您必须主动尝试不找到它们。我想不出任何其他方法,而不是使用Directory.GetDirectories(“C:\”)迭代整个C驱动器,然后使用Directory.GetFiles()遍历每个目录上的文件.请记住,一个大硬盘可能需要很长时间才能搜索到根目录。EnumerateFiles在这里是一个更好的选择。DirectoryInfo.GetDirectory不是递归的,只会将目录降低一级。@Mary这是正确的,这就是函数
FileExists
递归调用自身的原因。@RobertBaron明白了。我不擅长递归。谢谢。你可能会遇到堆栈问题,这取决于你的深度。当我运行这段代码时,我遇到了一些错误。首先是DirectoryNotFoundException,这似乎是不可能的。它怎么能找不到刚刚找到的目录呢?这是一条看起来很糟糕的小路,有两块瓷砖。我什么都没做,好像是来自微软;与Skype有关。无论如何,我将错误添加到Catch部分,然后重试。下面是使其崩溃的路径C:\Windows\softwaredistribution.bak2\Download\9EAEE87ABABA23B313C5B7697B9A103AD\AMD64\U Microsoft.ModernApps.Client.core~~AMD64~~10.0.17134.1\Microsoft.skypeapp\U kzf8qxf38zg5c\Microsoft.skypeapp\U 12.13.274.0\U x64\UZF8QXF38ZG5C\skypeapp\designs\flags\smallSecond试用产生的异常管理调试助手“ContextSwitchDeadlock”Message=托管调试助手“ContextSwitchDeadlock”:“CLR已经60秒无法从COM上下文0x4066af40转换到COM上下文0x4066ae18。拥有目标上下文/单元的线程很可能正在执行非泵送等待或在不泵送Windows消息的情况下处理长时间运行的操作。第2部分:这种情况通常会对性能产生负面影响,甚至可能导致应用程序变得无响应或内存使用不断累积随着时间的推移。为避免此问题,所有单线程单元(STA)线程都应使用泵送等待原语(如CoWaitForMultipleHandles),并在长时间运行的操作中定期泵送消息。“我单击继续执行,没有进一步的问题,但它从未找到我的文件。”。
Dim dInfo = New DirectoryInfo("C:\")
Dim matches = dInfo.GetFilesDepthFirst(filePattern:="somefile.dll")
Dim exists = matches.Any()