1

I created a DLL in C#, and try to call a function from it. But it has an error:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

My DLL code:

using System;
namespace Draf_Lib
{
    public class Class1
    {
        public static int add(int x, int y) { return x + y; }
    }
}

And here is my C# code to call the function from it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//using MacroLib;
using Draf_Lib;

namespace Draft1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
            Class1 c = new Class1();
            int b = Class1.add(9, 8);
            Console.WriteLine(b);
        }
    }
}

I already included the library as a reference. Can anyone suggest me a way to fix it?

enter image description here

TomC
  • 33
  • 6
  • Check [This](https://stackoverflow.com/questions/74367760/could-not-load-file-or-assembly-system-runtime-version-7-0-0-0-after-ins),[This](https://stackoverflow.com/questions/42755274/visual-studio-2017-could-not-load-file-or-assembly-system-runtime-version-4) and [This](https://learn.microsoft.com/en-us/answers/questions/902363/cannot-load-file-or-assembly-system-runtime) out. – Abol_Fa Jun 13 '23 at 04:15
  • it seems to be complaining about a different assembly. fuslogvw will show you what assemblies its loading and where its looking for them. You can also use sysinternals procmon to see what file io is being executed, so again you can see where its looking and for what – pm100 Jun 13 '23 at 04:50
  • Is your program and your assembly (DLL) targetting the same .NET? – Palle Due Jun 13 '23 at 08:21

1 Answers1

1

This is because you chose the wrong target framework when creating the project. Here's an example where I reproduce your problem:

  1. First, I selected Class Library when creating the class library, and the target framework was selected as .net 6.0

enter image description here

enter image description here

  1. Then I created a Console App (.net framework) and selected the target framework as .net framework 4.7.2

enter image description here

  1. The following error was reported when I introduced the dll and ran the project.

enter image description here

In fact, you should choose Console App when creating a console program, as follows. The target framework can choose .net 6.0.

enter image description here

wenbingeng-MSFT
  • 1,546
  • 1
  • 1
  • 8