【问题标题】:How to check if a file/folder exists in the C drive VB.NET如何检查C盘VB.NET中是否存在文件/文件夹
【发布时间】:2019-06-02 15:27:25
【问题描述】:

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

【问题讨论】:

  • “所有 C 盘”是什么意思?您是指 C 盘上的任何地方吗?
  • 互联网上有递归文件搜索的例子。您必须积极尝试不找到它们。
  • 除了用 Directory.GetDirectories("C:\") 迭代整个 C 盘,然后用 Directory.GetFiles( )。
  • 请记住,大硬盘驱动器可能需要很长时间才能搜索完

标签: vb.net


【解决方案1】:

这是我改编自 How to: Iterate Through a Directory Tree 的代码。在示例中,如果在 C 驱动器上的任何位置找到文件 bit32.txtFileExists() 函数将返回 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

【讨论】:

  • 我认为root.EnumerateFiles在这里会是更好的选择。
  • DirectoryInfo.GetDirectories 不是递归的,只会将目录向下一层。
  • @Mary 没错,这就是函数FileExists 递归调用自身的原因。
  • @RobertBaron 知道了。我在递归方面很糟糕。谢谢。
  • 您可能会遇到爆栈问题,具体取决于您的深度。
【解决方案2】:

您可以在 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

然后你可以像这样相当容易地调用它:

Dim dInfo = New DirectoryInfo("C:\")
Dim matches = dInfo.GetFilesDepthFirst(filePattern:="somefile.dll")
Dim exists = matches.Any()

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

【讨论】:

  • 当我运行这段代码时,我遇到了一些错误。首先是 DirectoryNotFoundException,这似乎是不可能的。它怎么找不到它刚刚找到的目录?这是一条看起来很糟糕的路径,其中包含双波浪线。我什么都没做,它似乎来自微软;与Skype有关。无论如何,我将错误添加到 Catch 部分并再次尝试。
  • 这里是崩溃的路径 C:\Windows\softwaredistribution.bak2\Download\9eaee87abab23b313c5b7697b9a103ad\AMD64_Microsoft.ModernApps.Client.core~~AMD64~~10.0.17134.1\microsoft.skypeapp_kzf8qxf38zg5c\microsoft. skypeapp_12.13.274.0_x64__kzf8qxf38zg5c\skypeapp\designs\flags\small
  • 第二次尝试未产生异常 Managed Debugging Assistant 'ContextSwitchDeadlock' Message=Managed Debugging Assistant 'ContextSwitchDeadlock' : 'CLR 在 60 秒内无法从 COM 上下文 0x4066af40 转换到 COM 上下文 0x4066ae18。拥有目标上下文/单元的线程很可能正在执行非泵送等待或处理非常长时间运行的操作而不泵送 Windows 消息。
  • 第 2 部分:这种情况通常会对性能产生负面影响,甚至可能导致应用程序变得无响应或内存使用量随时间不断累积。为避免此问题,所有单线程单元 (STA) 线程都应使用泵送等待原语(例如 CoWaitForMultipleHandles)并在长时间运行的操作期间定期泵送消息。'
  • 我点击继续执行,没有其他问题,但它从未找到我的文件。
猜你喜欢
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 2012-02-05
  • 2013-03-12
  • 1970-01-01
  • 2011-01-15
相关资源
最近更新 更多