Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

703
Views
C# File.Exists returns false, file does exist

Using VS 15, C# with .Net 4.5.2
The computer is on an AD network, with the ad name "AD".
This problem happens with AD normal-user rights, AD admin rights, and local admin rights. It doesn't matter what rights the program gets, the same problem occurs.

Our test file is "C:/windows/system32/conhost.exe".
The file above exists, it is very much existing. I can see it with explorer.

This is the file in explorer:
enter image description here

This is the file properties:
enter image description here

You can see that it is there, right?
The following cmd command checks if the file exists:

IF EXIST "C:\windows\system32\conhost.exe" (echo does exist) ELSE (echo doesnt exist)

It returns "does exist" as promised.

The following C# code checks if the file exists:

FileInfo file = new FileInfo("C:/windows/system32/conhost.exe");
MessageBox.Show(file.Exists + "");

This returns "False".

This code also returns "False":

MessageBox.Show(File.Exists("C:/windows/system32/conhost.exe") + "");

This code also doesn't find it:

foreach (string file in Directory.GetFiles("C:/windows/system32/"))
{
    //conhost is NEVER mentioned, like it doesn't exist
}

This code also doesn't find it:

foreach (string file in Directory.EnumerateFiles("C:/windows/system32/"))
{
    //conhost is NEVER mentioned, like it doesn't exist
}

False, False, False:

MessageBox.Show(File.Exists("C:/windows/system32/conhost.exe") + "");
MessageBox.Show(File.Exists("C:\\windows\\system32\\conhost.exe") + "");
MessageBox.Show(File.Exists(@"C:\windows\system32\conhost.exe") + "");

What am I doing wrong?
Extra note: I copied conhost to C:\conhost.exe, and my program can find that without problem. My program also finds other files in system32, just not conhost and a few others. For example, it finds "connect.dll" which is in system32, so it's not the directory's read permission.
More extra notes: conhost.exe and connect.dll has the same security attributes (Security tab in the file properties).

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If you are using x64 system, you will have different content of the c:\Windows\System32 directory for x86 and x64 applications. Please be sure that you are using same architecture running batch file and your C# app.

about 4 years ago · Santiago Trujillo Report

0

In the MSDN documentation for System.IO.File.Exists(path), it states:

If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.

For this reason, we can safely assume that your application does not have read access to that specific file. Check the security settings and grant read access if not already done so.

Build your application (in release mode) and run as administrator.

about 4 years ago · Santiago Trujillo Report

0

This is the problem that come over 64-bit operating system... here is a work around,

go to the project's properties > click on build tab > untick Prefer 32-bit

after that, it should work correctly over 64-bit os.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!