PathFileExists always returning FALSE

closed account (NUCkSL3A)
Hello, I'm having trouble with a bit of code. Here it is:


1
2
3
4
5
6
7
8
9
10
LPCWSTR path = (L"C:\\windows\\system32\\hal.dll");

	
	if (PathFileExists(path) == TRUE)
	{
	MessageBox(NULL, L"This file exists", L"File exists", MB_OK);
	}
	if (PathFileExists(path) == FALSE)
	{
	MessageBox(NULL, L"This file doesn't exist", L"File doesn't exist", MB_OK);


The problem is that it always returns FALSE. No matter what. I have searched for hours and have not found any solution. Most people simply post a minute later that they figured it out, without actually posting the code or explaining. I am wondering what can I do so it actually says whether or not it exists. (And yes, hal.dll exists.)
Last edited on
Try with some other filenames:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <Windows.h>
#include <Shlwapi.h>

#pragma comment(lib, "Shlwapi.lib")

using namespace std;

int main ()
{
  LPCWSTR path = (L"C:\\Windows\\System32\\notepad.exe");

  if (PathFileExists (path))
  {
    MessageBox (NULL, L"This file exists", L"File exists", MB_OK);
  }
  else
  {
    MessageBox (NULL, L"This file doesn't exist", L"File doesn't exist", MB_OK);
  }
  return 0;
}
This may be something to do with protected operating system files not being visible via this API.

Try finding the same file when copied to a neutral folder (like your downloads folder) - should work

Try finding files with different (non-protected) extensions in C:\Windows\System32 - should work

Try finding other files with protected extensions in C:\Windows\System32 - should not work

This should prove that there's some Windows magic preventing you from finding certain files in certain folders.

Why is this so? Well...
closed account (NUCkSL3A)
hmm... Ok, I'll try these and see if it will work. Thx. Update: Yep, it wouldn't find the system files. I tried it on a text document on my desktop. Sure enough, it worked. Problem solved, thanks a lot!
Last edited on
Topic archived. No new replies allowed.