[.net] Getting the PublicKeyToken of .Net assemblies

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.

This question is related to .net visual-studio visual-studio-2010 public-key

The answer is


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


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

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

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);
            }

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.

2) 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


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">
...

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;
}

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


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 .


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! ;))


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


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.


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.


Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to visual-studio

VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to download Visual Studio Community Edition 2015 (not 2017) Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio Code pylint: Unable to import 'protorpc' Open the terminal in visual studio? Is Visual Studio Community a 30 day trial? How can I run NUnit tests in Visual Studio 2017? Visual Studio 2017: Display method references

Examples related to visual-studio-2010

variable is not declared it may be inaccessible due to its protection level SSIS Excel Connection Manager failed to Connect to the Source This project references NuGet package(s) that are missing on this computer Gridview get Checkbox.Checked value error LNK2038: mismatch detected for '_MSC_VER': value '1600' doesn't match value '1700' in CppFile1.obj What is the difference between Visual Studio Express 2013 for Windows and Visual Studio Express 2013 for Windows Desktop? Attach (open) mdf file database with SQL Server Management Studio What is and how to fix System.TypeInitializationException error? Could not load file or assembly "Oracle.DataAccess" or one of its dependencies IIS error, Unable to start debugging on the webserver

Examples related to public-key

key_load_public: invalid format Verify host key with pysftp Github permission denied: ssh add agent has no identities git push: permission denied (public key) Heroku 'Permission denied (publickey) fatal: Could not read from remote repository' woes Using scp to copy a file to Amazon EC2 instance? How to ssh connect through python Paramiko with ppk public key Adding a public key to ~/.ssh/authorized_keys does not log me in automatically Permission denied (publickey) when deploying heroku code. fatal: The remote end hung up unexpectedly GitHub: Permission denied (publickey). fatal: The remote end hung up unexpectedly