System.IO.FileNotFoundException: Could not load file or assembly 'Shared, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. means the app
was compiled against Shared.dll and that file is not in the folder the app runs from.
Run the app from its complete build output, or publish it and copy the whole publish folder.
The usual trigger is copying part of bin: the app's own files without the DLLs of the
projects and packages it references. dotnet publish -c Release -o publish gathers every
file the app needs into one folder; copy that folder as a unit.
The error
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
at Program.<Main>$(String[] args)
Why it happens
A project reference compiles the library into its own DLL, and the build copies that DLL next to the
app. After dotnet build the output folder held ClinicApp.dll,
ClinicApp.exe and Shared.dll, among others. The app does not contain the
library's code; it records the assembly name and version and asks the runtime to load it when code
needs it. With Shared.dll moved out of the folder, the load had nowhere to look.
The program's first line was Console.WriteLine("Starting"), and it never printed. The
load failed while Main itself was being prepared, because Main calls into
the missing library, so the stack trace ends at Program.<Main>$ and the process
exited with code -532462766. Do not hunt for the failing line; look for the missing file named in the
message.
The fix
dotnet publish -c Release -o publish
dir /b
ClinicApp.deps.json
ClinicApp.dll
ClinicApp.exe
ClinicApp.pdb
ClinicApp.runtimeconfig.json
Shared.dll
Shared.pdb
.\ClinicApp.exe
Starting
Hello, Maria Garcia
Moving Shared.dll back into the build folder fixed it the same way. Copying only the
exe gives a different error, because ClinicApp.exe is a small launcher for
ClinicApp.dll: The application to execute does not exist:
'...\exeonly\ClinicApp.dll'.
This test reproduced the missing-file case only. If the file is present but the message names a
version you did not expect, find out which project brings that assembly in. For a package,
dotnet nuget why prints the path:
dotnet nuget why ClinicApp.csproj Npgsql
Project 'ClinicApp' has the following dependency graph(s) for 'Npgsql':
[net10.0]
└── Shared
└── Npgsql (v10.0.3)
How it was reproduced
On Windows 11 with .NET SDK 10.0.402 and runtime 10.0.13: a class library from dotnet new
classlib -n Shared and a console app from dotnet new console -n ClinicApp, joined
with dotnet add reference. After dotnet build, Shared.dll was
moved out of bin\Debug\net10.0 with Move-Item, and both
.\ClinicApp.exe and dotnet ClinicApp.dll printed the same exception. For
the last step, Npgsql 10.0.3 was added to the library to show a transitive path.
Frequently asked
- Why does my .NET app fail when I copy only the exe?
- The exe is a launcher for the app's DLL. Copied alone it fails with The application to execute does not exist. Copied with the app DLL but without referenced DLLs, it fails with Could not load file or assembly. Copy the whole build or publish folder.
- Does dotnet publish include the DLLs of referenced projects?
- Yes. In this test the publish folder held Shared.dll and Shared.pdb next to ClinicApp.dll, and the app ran from it.
- Why did my program crash before the first Console.WriteLine?
- The missing assembly was needed to prepare Main itself, because Main calls into it. The load fails before any line of Main runs, so nothing is printed and the stack trace ends at Main.
More decoded errors are in the Fixes category.