-1

i'm trying to create a simple anti-cheat which detects external cheats in all the C drive, but i don't know how to check if a determinate file exists in all the C drive, any help?

  • What do you mean by "all the C drive"? Do you mean **anywhere** on the C drive? – RobertBaron Jun 02 '19 at 15:34
  • 1
    There are examples of recursive file searches lettering the internet. You'd have to actively try to not find them. – jmcilhinney Jun 02 '19 at 16:12
  • I can't think of any other way rather than iterating the whole C drive with Directory.GetDirectories("C:\"), and then looping through the files on each directory with Directory.GetFiles(). – Mariano Luis Villa Jun 02 '19 at 16:38
  • Bear in mind that a large hard drive could take quite a long time to search through – ADyson Jun 02 '19 at 17:29

2 Answers2

0

Here is code that I adapted from How to: Iterate Through a Directory Tree. In the example, the FileExists() function returns True if the file bit32.txt is found anywhere on the C drive. Not all files and directories may be accessible. In that case, the function display the paths of these files and directories. You may want to adapt the code to do whatever you want in these cases.

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
RobertBaron
  • 2,817
  • 1
  • 12
  • 19
0

You can have an extension on DirectoryInfo that non-recursively iterates through the root directory looking for files.

<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

You could then call it fairly easily like this:

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

Also, the more specific you are at the starting directory, the quicker it will run. Usually searching from the root of C:\ is a very slow and bad idea.

Parrish Husband
  • 3,148
  • 18
  • 40
  • When I ran this code I got a few errors. First was DirectoryNotFoundException which seems impossible. How can it not find a Directory it just found? It was a terrible looking path with double tildes among other things. Nothing I did, it seems to have come from Microsoft; something to do with Skype. Anyway I added the error to the Catch section and tried again. – Mary Jun 02 '19 at 19:05
  • Here is the path that crashed it 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 – Mary Jun 02 '19 at 19:06
  • Second try produced not an exception Managed Debugging Assistant 'ContextSwitchDeadlock' Message=Managed Debugging Assistant 'ContextSwitchDeadlock' : 'The CLR has been unable to transition from COM context 0x4066af40 to COM context 0x4066ae18 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. – Mary Jun 02 '19 at 19:08
  • part 2: This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.' – Mary Jun 02 '19 at 19:09
  • I clicked to continue execution and no further problems but it never did find my file. – Mary Jun 02 '19 at 19:10
  • @Mary `DirectoryNotFound` is a possible exception inside the `EnumerateDirectories/GetDirectories` methods. "The path encapsulated in the DirectoryInfo object is invalid". – Parrish Husband Jun 02 '19 at 19:13
  • If you're interested in posting the code on pastebin I could look at the implementation and see if anything jumps out. – Parrish Husband Jun 02 '19 at 19:15
  • I assume the Debugging Assistant error was after attempting to inspect the matches inside the debugger? If you want that to work, you'll need to add a `ToList()` on the call, but obviously this is not ideal when deployed. Also see more on this problem here: https://stackoverflow.com/questions/578357/visual-studio-contextswitchdeadlock – Parrish Husband Jun 02 '19 at 19:19