Question

Suppose I want to display the full name of an assembly, given only a path to the assembly file.

I know I could write a command line program or powershell script easily enough, but does the runtime include a command that I could use? (I've seen gacutil do it for assemblies that are in the gac, but about assemblies that are not registered?)

Was it helpful?

Solution

The main problematic part of a full-name is usually the public key token. You can obtain it by entering this into the Visual Studio Command Prompt Window:

sn -T path\to\your.dll

Personally, I find it easier just to load the dll into reflector, though. That shows the full name in the bottom panel.

OTHER TIPS

I think you're looking for System.Reflection.AssemblyName.GetAssemblyName:

AssemblyName name = AssemblyName.GetAssemblyName(@"C:\path\assembly.dll");
Console.WriteLine(name.FullName);

Unlike the solution using Assembly.Load, this will not load the assembly into your AppDomain. This will allow you to get the AssemblyName for an assembly without having to resolve any references that assembly has, and without executing any code (e.g. module static constructors) in the assembly. It will even let you get the AssemblyName of an assembly that targets a version of the .NET Framework that differs from the version on which your application is running.

You can get this via AssemblyName:

var name = AssemblyName.GetAssemblyName(path);
var fullname = name.FullName;

This avoids actually loading the assembly into your process.

For another answer, you can do this with PowerShell:

[System.Reflection.AssemblyName]::GetAssemblyName($PathToAssembly).FullName

This has the advantage of not needing to have Visual Studio installed just to get sn.exe, and also that it returns the complete strong name for the assembly rather than just the public key token.

It's easy enough to wrap that in a cmdlet script:

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$true,Position=0)]
    $Path
)

Process {
    $FullPath = [System.IO.Path]::GetFullPath($Path);
    return [System.Reflection.AssemblyName]::GetAssemblyName($FullPath).FullName;
}
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top