How to extract assembly name from DLL file in C#

There are situations where you have a DLL file/s in a project that does not necessarily have the same name or you are downloading that DLL file/s from a third party or other sources that are not necessary again to have the same name.

For this reason, I renamed Newtonsoft.Json.dll to 6b9aeacd-0521-444b-a5eb-a8c58b065c6e.dll and put it up on my blog.

Now lets download the file:

var baseDirectory = @"D:\project\dlls";
var link = "";
var uri = new Uri(link);
var fileName = Path.GetFileName(uri.LocalPath);
var path = Path.Combine(baseDirectory, fileName);

if (!new FileInfo(path).Exists)
    HttpClient client = new HttpClient();
    var response = await client.GetAsync(uri);
    await using var fs = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite);
    await response.Content.CopyToAsync(fs);

Now let extract the original assembly name of the file which is Newtonsoft.Json.dll. This can be done by 2 ways.

One way is by loading the file as Assembly object which is part of System.Reflection and the other is by using FileVersionInfo object which is part of System.Diagnostics.

Lets take the Assembly object way:

Assembly dll = Assembly.LoadFile(path);
var dllName = dll.ManifestModule.ScopeName;

This will output Newtonsoft.Json.dll.

Now lets take the FileVersionInfo way:

FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(path);
var dllName = fileVersionInfo.OriginalFilename;

This will output Newtonsoft.Json.dll.

Both can extract assbley name but each of them has own use cases. For example Assmbely has ExportedTypes that gives a list of all types inside Nettonsoft.Json.dll and more features, where FileVersionInfo has file features.

Read more about:

  • FileVersionInfo:
  • Assembly:

Leave a Comment