Using C#, .NET 9 and I have added the nuget package for System.Data.Oledb
I have included using System.Data.Oledb
The file System.Data.Oledb.dll
is in the same folder as the exe.
When I launch the exe in debug mode (debugger attached) everything works.
When I launch the same exe without debugger I get the error
Could not load file or assembly 'System.Data.OleDb, Version=9.0.0.3, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
Why can't it find the assembly without debugger attached?
Note: I'm running in EXCATLY the same location - just with the debugger or without the debugger.
Using C#, .NET 9 and I have added the nuget package for System.Data.Oledb
I have included using System.Data.Oledb
The file System.Data.Oledb.dll
is in the same folder as the exe.
When I launch the exe in debug mode (debugger attached) everything works.
When I launch the same exe without debugger I get the error
Could not load file or assembly 'System.Data.OleDb, Version=9.0.0.3, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
Why can't it find the assembly without debugger attached?
Note: I'm running in EXCATLY the same location - just with the debugger or without the debugger.
Share Improve this question edited Mar 13 at 23:55 Michael T asked Mar 13 at 23:24 Michael TMichael T 7918 silver badges23 bronze badges 5- Did you ensure that System.Data.OleDb.dll is copied to the output directory? If yes, then If your app is not running in a development environment, dependencies may be missing on the target machine. Considering your answer I may figure it out. – Emad Kerhily Commented Mar 13 at 23:51
- @EmadKerhily I've updated my question to make it clear I'm launching the app from the same location each time. – Michael T Commented Mar 13 at 23:57
- Let's try my answer and test some solutions step by step – Emad Kerhily Commented Mar 14 at 0:10
- 1 @EmadKerhily Thanks your answer explicitly loading the assembly at runtime combined with this stackoverflow/questions/79164448/… helped me figure it out. The build process now copies the specific files from the runtimes subfolder. But I have no idea why it worked under debug mode. – Michael T Commented Mar 14 at 1:07
- Because, when you run in debug mode, Visual Studio ensures dependencies are loaded – Emad Kerhily Commented Mar 14 at 1:20
1 Answer
Reset to default 1Even though you have System.Data.OleDb.dll
in the same folder, it’s worth ensuring it's copied correctly during build.
Modify your .csproj
file to always copy the DLL to the output:
<ItemGroup>
<None Update="System.Data.OleDb.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
clean and rebuild the project and let me know if it works.
Alternatively, you can load assembly at runtime explicitly in your program.cs:
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
if (args.Name.Contains("System.Data.OleDb"))
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System.Data.OleDb.dll");
return File.Exists(path) ? Assembly.LoadFrom(path) : null;
}
return null;
};