visual-studio.net-core

Cannot run dotnet.exe from CMD or compile in Visual Studio 2022


I ran a command prompt as an admin, and executed the following command:

C:\Windows\System32\dotnet.exe

A dialog popped up saying:

This app can't run on your PC
To find a version for your PC, check with the software publisher.

Then, in the prompt, it outputted:

Access is denied

Meanwhile, I was unable to compile any solution in Visual Studio 2022, though I can't recall the specific error message.

I decided to wipe everything (the .NET SDK, Visual Studio 2022, and many temporary folders) and reinstalled Visual Studio 2022.

Unfortunately, the situation worsened. Now, I can't even load .NET projects, getting an error:

The SDK 'Microsoft.NET.Sdk' specified could not be found.

Before posting this question, I tried various solutions found online, such as creating a new folder in Program Files (x86) and adjusting the order of paths in the system variable Path.

Thank you in advance for your help.

Update on 28th Apr:

As per requested by @phuzi, I ran the command (I can execute it from where it is installed):

C:\Program Files\dotnet>dotnet.exe --list-sdks
9.0.201 [C:\Program Files\dotnet\sdk]

My projects target .NET 8. That explains it now.

However, when I reinstall my VS2022 (through VS installer), I ticked .NET 8. I have just checked it in the installer, in the tab Individual components, in the .NET section, I still have the following items ticked:

So, now the question is, why did the VS installer not install the SDK properly?

Thank you for your help, guys.

2nd update on 28th Apr:

I have tried to remove .NET 8.0 Runtime (LTS) and install it back in VS Installer, and that did not fix it. Again, it gets worse, now I see my projects are rendered as "ProjectName(incompatible)" in the explorer.

Update on 30th Apr Please note that, the option of .NET 8.0 SDK no longer exists in the installer, and I can only see .NET SDK, and it is ticked and installed, but the problem remains.

From workload point of view, ASP.NET and web development, Azure development and .NET desktop development are ticked.


Solution

  • Finally, I have figured it out.

    First thing first, it seems that the VS Installer cannot install the .NET 8 SDK for you anymore, and I had to install the SDK outside it, I downloaded dotnet-sdk-8.0.408-win-x64.exe from Microsoft's site and installed the SDK for VS.

    Secondly, the culprit is an accidental copy of dotnet.exe, it was made under C:\Windows\System32, and somehow it is corrupted, for its size is 0 byte. I might have made the copy by accidental drag-and-drop, but I cannot be certain, as it should not be 0 byte in that case. This copy is not supposed to be there, and it caused me seeing the error "This app can't run on your PC" (please refer to the question). On the other hand, I realized that Visual Studio itself does not seem to know the location of dotnet.exe, and when it builds, it only relies on the system variables to find out where it is and execute dotnet.exe, and because there is a copy C:\Windows\System32\dotnet.exe, and C:\Windows\System32 is in the variable Path, VS 2022 simply invokes C:\Windows\System32\dotnet.exe, which caused the error "The SDK 'Microsoft.NET.Sdk' specified could not be found." Looking back the history, the problem started exactly the day when this copy was made.

    I used tools like process monitor and the command 'where dotnet' for troubleshooting. In summary, I think Microsoft should improve the error message to be more descriptive.

    I want to thank those people who tried to help.