0

I have the current file structure in my Visual Studio project:

  • MyProject/
    • MyStartupProject/
      • bin/
        • Debug/
      • Program.cs
    • DependencyProject/
      • bin/
      • dlls/
        • MyAssembly.dll
      • Code.cs

My Main() function is inside MyProject/MyStartupProject/Program.cs. Inside Code.cs is a line var assembly = Assembly.Load("MyAssembly");, which is supposed to load MyProject/DependencyProject/dlls/MyAssembly.dll, but instead it causes the error

System.IO.FileNotFoundException: 'Could not load file or assembly 'MyAssembly' or one of its dependencies.
The system cannot find the file specified.'

However, everything runs perfectly fine if I copy MyAssembly.dll into MyProject/MyStartupProject/bin/Debug. How do I fix this reference so the project can find it under the dlls directory?

wheeeee
  • 1,046
  • 2
  • 14
  • 33
  • 1
    you need to add it as a project resource, then it'll get copied across the profiles – jazb Jan 27 '22 at 07:24
  • how do I add it as a project resource? @Jazb – wheeeee Jan 27 '22 at 08:06
  • The build system doesn't know that you have a dependency on this DLL, so won't copy it for you. You [have to help](https://stackoverflow.com/a/21760295/17034). Or use LoadFrom() instead of Load(). Or just use a project reference and use types from the assembly in your code. – Hans Passant Jan 27 '22 at 08:53

2 Answers2

1

Try to Change the status of MyAssembly.dll to Copy if new.

enter image description here

Update:

enter image description here

Add file

enter image description here

enter image description here

Update2:Is there a way I can get it to copy straight into Debug/ without moving the original out of the dlls/ directory?

Two solutions:

  1. Change var assembly = Assembly.Load("MyAssembly"); to var assembly = Assembly.LoadFrom("dlls\MyAssembly.dll");.

  2. Add

<Target Name="BeforeBuild">
        <Copy
            SourceFiles="dlls\MyAssembly.dll"
            DestinationFolder="bin\Debug"
        />
    </Target>

to csproj.

csproj:

  1. Unload Project:

enter image description here

2.Double-click Project:

enter image description here

Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21
  • I have a "Copy Local" which is already set to true, but no "Copy to Output Directory" option. I am using .NET Framework if that makes a difference – wheeeee Jan 27 '22 at 08:05
  • Please check my updates, did you right-click on this property? I am .Net Framework 4.8. – Jiale Xue - MSFT Jan 27 '22 at 08:25
  • I found that the assembly was copied to `MyStartupProject/bin/Debug/dlls/MyAssembly.dll`, rather than `MyStartupProject/bin/Debug/MyAssembly.dll` and the error persists. Is there a way I can get it to copy straight into `Debug/` without moving the original out of the `dlls/` directory? – wheeeee Jan 27 '22 at 20:42
  • Please check my updates, If you don't mind, you could click '✔' to mark my reply as the accepted answer. It will also help others to solve the similar issue. – Jiale Xue - MSFT Jan 28 '22 at 03:03
  • I found another way to resolve my error so I did not get a chance to try your update – wheeeee Jan 31 '22 at 23:32
  • @wheeeee If you try to check my update you will find your answer in my latest update. – Jiale Xue - MSFT Feb 01 '22 at 01:13
1

I resolved my issue by adding the following to my DependencyProject.csproj

<ContentWithTargetPath Include="dlls\MyAssembly.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <TargetPath>MyAssembly.dll</TargetPath>
</ContentWithTargetPath>

This causes the dlls to be automatically copied into MyStartupProject/bin/Debug/.

wheeeee
  • 1,046
  • 2
  • 14
  • 33