Getting the PublicKeyToken of .Net assemblies

.NetVisual StudioVisual Studio-2010Public Key

.Net Problem Overview


What is the simplest way to find the Public-Key-Token of an assembly?

The simplest way I can think of would be a simple right-click, get public key, but this functionality isn't there, maybe there is a Visual Studio Extension for that?

I'm using Visual Studio 2010, if an extension is available.

.Net Solutions


Solution 1 - .Net

Open a command prompt and type one of the following lines according to your Visual Studio version and Operating System Architecture :

VS 2008 on 32bit Windows :

"%ProgramFiles%\Microsoft SDKs\Windows\v6.0A\bin\sn.exe" -T <assemblyname>

VS 2008 on 64bit Windows :

"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\bin\sn.exe" -T <assemblyname>

VS 2010 on 32bit Windows :

"%ProgramFiles%\Microsoft SDKs\Windows\v7.0A\bin\sn.exe" -T <assemblyname>

VS 2010 on 64bit Windows :

"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v7.0A\bin\sn.exe" -T <assemblyname>

VS 2012 on 32bit Windows :

"%ProgramFiles%\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\sn.exe" -T <assemblyname>

VS 2012 on 64bit Windows :

"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\sn.exe" -T <assemblyname>

VS 2015 on 64bit Windows :

"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\sn.exe" -T <assemblyname>

Note that for the versions VS2012+, sn.exe application isn't anymore in bin but in a sub-folder. Also, note that for 64bit you need to specify (x86) folder.

If you prefer to use Visual Studio command prompt, just type :

sn -T <assembly> 

where <assemblyname> is a full file path to the assembly you're interested in, surrounded by quotes if it has spaces.

You can add this as an external tool in VS, as shown here:
http://blogs.msdn.com/b/miah/archive/2008/02/19/visual-studio-tip-get-public-key-token-for-a-stong-named-assembly.aspx

Solution 2 - .Net

another option:

if you use PowerShell, you can find out like:

PS C:\Users\Pravat> ([system.reflection.assembly]::loadfile("C:\Program Files (x86)\MySQL\Connector NET 6.6.5\Assemblies\v4.0\MySql.Data.dll")).FullName

like

PS C:\Users\Pravat> ([system.reflection.assembly]::loadfile("dll full path")).FullName

and will appear like

> MySql.Data, Version=6.6.5.0, Culture=neutral, > PublicKeyToken=c5687fc88969c44d

Solution 3 - .Net

If the library is included in the VS project, you can check .cproj file, e.g.:

<ItemGroup>
    <Reference Include="Microsoft.Dynamic, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
...

Solution 4 - .Net

If you have included the assembly in your project, you can do :

            var assemblies =
                AppDomain.CurrentDomain.GetAssemblies();
                

            foreach (var assem in assemblies)
            {
                    Console.WriteLine(assem.FullName);
            }

Solution 5 - .Net

You can get this easily via c#

private static string GetPublicKeyTokenFromAssembly(Assembly assembly)
{
	var bytes = assembly.GetName().GetPublicKeyToken();
	if (bytes == null || bytes.Length == 0)
		return "None";

	var publicKeyToken = string.Empty;
	for (int i = 0; i < bytes.GetLength(0); i++)
		publicKeyToken += string.Format("{0:x2}", bytes[i]);

	return publicKeyToken;
}

Solution 6 - .Net

You can add this as an external tool to Visual Studio like so:

Title:

Get PublicKeyToken

Command:

c:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\sn.exe

(Path may differ between versions)

Arguments:

-T "$(TargetPath)"

And check the "Use Output window" option.

Solution 7 - .Net

The simplest way for me is to use ILSpy.

When you drag & drop the assembly on its window and select the dropped assembly on the the left, you can see the public key token on the right side of the window.

(I also think that the newer versions will also display the public key of the signature, if you ever need that one... See here: https://github.com/icsharpcode/ILSpy/issues/610#issuecomment-111189234. Good stuff! ;))

Solution 8 - .Net

In case someone was looking for the assembly Public Key (like me), not the Public Key Token - using sn.exe works great, except you have to use -Tp switch, which will return both the Public Key and Public Key Token - more at https://msdn.microsoft.com/en-us/library/office/ee539398(v=office.14).aspx .

Solution 9 - .Net

  1. The command is C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\sn -T {your.dll}

In the above example, the Microsoft SDK resides in C:\Program Files\Microsoft SDKs\Windows\v6.0A. Your environment may differ.

  1. To get the public key token of any of your project, you can add sn.exe as part of your External Tools in Visual Studio. The steps are shown in this Microsoft link: How to: Create a Tool to Get the Public Key of an Assembly

Solution 10 - .Net

As an alternative, you can also use linq like this -

    string key = string.Join ("",assembly
                                .GetName()
                                .GetPublicKeyToken()
                                .Select (b => b.ToString ("x2")));

Solution 11 - .Net

Another options is to use the open source tool NuGet Package Explorer for this.

From a Nuget package (.nupkg) you could check the PublicKeyToken, or drag the binary (.dll) in the tool. For the latter select first "File -> new"

enter image description here

Solution 12 - .Net

An alternate method would be if you have decompiler, just look it up in there, they usually provide the public key. I have looked at .Net Reflector, Telerik Just Decompile and ILSpy just decompile they seem to have the public key token displayed.

Solution 13 - .Net

You can use the Ildasm.exe (IL Disassembler) to examine the assembly's metadata, which contains the fully qualified name.

Following MSDN: https://msdn.microsoft.com/en-us/library/2exyydhb(v=vs.110).aspx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmoi_memeView Question on Stackoverflow
Solution 1 - .NetdigEmAllView Answer on Stackoverflow
Solution 2 - .NetMujah MaskeyView Answer on Stackoverflow
Solution 3 - .Netuser2341923View Answer on Stackoverflow
Solution 4 - .Netapoorv020View Answer on Stackoverflow
Solution 5 - .NetAdam CavinessView Answer on Stackoverflow
Solution 6 - .NetDanny VarodView Answer on Stackoverflow
Solution 7 - .Netuser2173353View Answer on Stackoverflow
Solution 8 - .NetPawel GorczynskiView Answer on Stackoverflow
Solution 9 - .NetSydView Answer on Stackoverflow
Solution 10 - .NetYogiView Answer on Stackoverflow
Solution 11 - .NetJulianView Answer on Stackoverflow
Solution 12 - .Netbhupendra patelView Answer on Stackoverflow
Solution 13 - .NetKryszalView Answer on Stackoverflow