FileExists() returns false, even if file exists

10,619

Solution 1

Most likely this is down to file redirection. You have a 64 bit machine but from the 32 Delphi process, Windows\system32 actually redirects to Windows\Syswow64. So when you think you are asking for the existence of a file in Windows\system32, the system is actually reporting the existance (or otherwise) of a file in Windows\Syswow64.

If you really do need to see into the true 64 bit system32 then you need to disable file redirection. You can do this with the Wow64DisableWow64FsRedirection() function. Don't forget to switch it back on with Wow64RevertWow64FsRedirection(). Beware that disabling the redirector has wide reaching effects and can result in very strange behaviour so do so with care.

Solution 2

Not much information to go on, the code you are using might help, but could this be a 64 bit issue and that the dll is actually in the SysWOW64 folder? See here for a good description of the how this works.

Solution 3

You are almost certainly not specifying the full or valid relative path of the file in your FileExists call. LoadLibrary will search certain locations (those where dlls are expected to reside) for you, but FileExists will not. Supply the complete and correct path and FileExists will work correctly.

Solution 4

This is the most ridiculous reason, but if it can help just one person...

Make sure you didn't accidentally name the file something.dll.dll.

I just had a situation where I installed an application on a clients computer, and then the application couldn't find config.txt located in the same directory. This had been working fine on other computers, so of course I was stumped.

Turns out the "show file extensions" setting was turned off on this clients computer, and the file had actually been named config.txt.txt... in my defence, I spend 90% of the time on OSx, and 9.99% on my own Windows system, with "show file extensions" enabled since ages ago.

Share:
10,619
SevenEleven
Author by

SevenEleven

Updated on June 22, 2022

Comments

  • SevenEleven
    SevenEleven almost 2 years

    I want to check if a dll in System32 directory (Windows 7) exists. But even if it exists, FileExists() returns false. LoadLibrary returns a valid handle. In this case, I only want to check, if the files exists and visualize this information. Do you have a any tips to solve this?

  • Andreas Rejbrand
    Andreas Rejbrand over 12 years
    This is more like it. (Well, this and some silly typo, of course...)
  • Andreas Rejbrand
    Andreas Rejbrand over 12 years
    Now that I think about it, I am 99 % this is the problem. +1, definitely.
  • balazs
    balazs over 12 years
    Yes, probably this is the case, in the past I had something similar, the strange was that my total commander showed me the redirected folder too, after somebody told me it's because it is 32bit everything was clear.(you can still disable redirection there). Anyway just checked the TC site, now there's a 64bit beta.
  • Remy Lebeau
    Remy Lebeau over 12 years
    You can use the special "Sysnative" alias to access the 64-bit System32 folder without disabling filesystem redirection, eg: FileExists('C:\Windows\Sysnative\filename.dll')
  • David Heffernan
    David Heffernan over 12 years
    @Remy This is a good point, but if you wish to support XP 64 then you need to make sure that the relevant hotfix is installed.
  • Rudy Velthuis
    Rudy Velthuis over 12 years
    @David: if he is trying to access an actual file in "C:\Windows\System32" on a 64 bit system in order to load the library, it might actually make sense that it shouldn't work, as the file is very likely a 64 bit DLL.