Search code examples
windowsexefilecompare

windows cannot access one file (exe)


Related to an earlier question, I found that when Visual Studio builds a C# project, the EXE file cannot be run with message "access denied", but not always. This evening at home I could run an EXE that I could not run earlier today in the office. The EXE from a new build can not be run. But I saved both runs in separate folders, and ran COMP over both folders to find any differences. To my surprise I found that even COMP cannot access the new EXE:

screenshot of COMP on two folders showing a problem by one EXE

The COMP program just compares the file contents. It finds no difference between all files from both Builds, except that the new EXE file "cannot be read".

Because COMP just reads and compares file bytes, and does not try to run or execute the file contents, the virus scanner is not involved.

I looked at File Properties, even for the Security tab and Advanced button, requiring local admin mode, but could not find any differences between the EXE from both folders:

screenshot file properties of EXE from both folders

I tried to copy the EXE that 'could not be read', and succeeded:

screenshot of copied version of EXE that could not be read by COMP

But the file SandboxConsoleApp - kopie.exe could still not be run with message 'cant read'. So we CAN read the file in order to create a copy, but it can't be read to be compared with COMP, or to be run.

Of course I suspected Visual Studio to hang on to that EXE it just built, so I closed the Solution, and later also closed the Visual Studio app. In both cases, the problem EXE could still not be read.

So what is going on here? Why does Windows say it can't read the file for running or for input to COMP, but we can still "read" the file to make a copy, or to inspect its Properties.

And somehow, the status "can't read" seem to change over the course of several hours to a runnable EXE.

Versions: Visual Studio Professional 2022, Windows 10 Enterprise 21H2.

Thanks for any help!


Update:

After creating this post, the problem EXE is now a little over one hour old. And lo and behold, it can be read! And executed! Problem a little bit solved.

Now the question is:

How can I run the EXE built by Visual Studio without having to wait for over an hour?


Solution

  • The problem turned out to be the virus scanner, but it is too bad that that was not obvious. Only because we found a fix, we could establish the cause. The Windows Resource Monitor could not find the app that was locking my exe.

    In the Windows Settings, go to Virus and Threats Security. (I am guessing, because my Windows is set to the Dutch language).

    Then, click on Settings For Virus and Threats Security. Then, click on Exemptions. Then you see a list of files and folders that are exempt from inspection by the virus scanner. If your employer has allowed you to manage your own virus scanner, you can add the folder where your Visual Studio solution(s) live so that you can run and test your own applications.