0
Assembly assembly = Assembly.LoadFrom("System.Private.CoreLib.dll");

With C#, I am trying to load that dll, but it keeps saying:

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'C:\Users\erikw\source\repos\DictMain\DictMain\bin\Debug\net9.0\System.Private.CoreLib.dll'. The system cannot find the file specified.

... yet the file does exist in the application's directory!

C:\Users\erikw\source\repos\DictMain\DictMain\bin\Debug\net9.0>dir
 Volume in drive C is Windows-SSD
 Volume Serial Number is 745E-1B48

 Directory of C:\Users\erikw\source\repos\DictMain\DictMain\bin\Debug\net9.0

07/18/2025  03:28 PM    <DIR>          .
07/12/2025  07:21 PM    <DIR>          ..
07/18/2025  03:15 PM               416 DictMain.deps.json
07/18/2025  03:28 PM             5,120 DictMain.dll
07/18/2025  03:28 PM           155,136 DictMain.exe
07/18/2025  03:28 PM            10,836 DictMain.pdb
07/18/2025  03:15 PM               268 DictMain.runtimeconfig.json
07/12/2025  08:31 PM               303 dllKey.txt
07/18/2025  03:12 PM         1,601,536 System.Private.CoreLib.dll
               7 File(s)      1,773,615 bytes
               2 Dir(s)  3,804,758,093,824 bytes free

C:\Users\erikw\source\repos\DictMain\DictMain\bin\Debug\net9.0>
7
  • I noticed that in your unhandled exception message it specifies the entire path and file but without the .dll extension at the end. "..\Debug\net9.0\System.Private.CoreLib" Should it not include the extension in that message? Perhaps it is just looking for the wrong thing. Commented Jul 18 at 23:44
  • I tried loading the DLL with and without the .dll file extension same result Commented Jul 18 at 23:58
  • I posted the wrong error message, I got mixed up. I will correct my posted question. I was just testing the code without the .dll extension Commented Jul 19 at 0:04
  • The only other idea I've got is to provide the absolute path to the dll. Assembly.LoadFrom("C:\Users\erikw\source\repos\DictMain\DictMain\bin\Debug\net9.0\System.Private.CoreLib.dll"); Commented Jul 19 at 0:41
  • 1
    Why are you trying to load the runtime assembly? It should already be loaded. Commented Jul 19 at 5:52

2 Answers 2

2

You could use type from that library to get the assembly object.

For instance, string is defined in this assembly, so it's as easy as:

Assembly coreLibAssembly = typeof(string).Assembly;
Console.WriteLine(coreLibAssembly.FullName);

It outputs for me

System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e

Sign up to request clarification or add additional context in comments.

Comments

1

Per .NET documentation: The LoadFrom method has a number of disadvantages. Consider using Load instead.

This specific assembly, System.Private.CoreLib is already loaded when you run a .net console application. You can check it by running this in Program.cs:

using System.Reflection;

void ListAssemblies()
{
    Console.WriteLine("Assemblies:");
    foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
        Console.WriteLine(assembly.Location);
}
ListAssemblies();

No need to copy System.Private.CoreLib.dll file to the application's folder, you can delete it. The copy from your application's folder will not be used, because the assembly is already loaded from location like this: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\9.0.7\System.Private.CoreLib.dll.

You can pass to Load method either assembly name without .dll or file name with .dll.

Example:

Assembly a1 = Assembly.Load("System.Private.CoreLib"); // works without .dll
Assembly a2 = Assembly.Load("System.Private.CoreLib.dll"); // also works with .dll

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.