192

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.

shA.t
  • 16,580
  • 5
  • 54
  • 111
moi_meme
  • 9,180
  • 4
  • 44
  • 63
  • 4
    See this blog post from Kirk Evans how to get PublicKeyToken of an assembly within Visual Studio: http://blogs.msdn.com/b/kaevans/archive/2008/06/18/getting-public-key-token-of-assembly-within-visual-studio.aspx – Martin Buberl Jan 20 '11 at 15:03
  • 2
    Just installed EF6 and honestly don't even know where the assemblies are located. Crap like this takes hours of my day. – froggythefrog Jun 09 '14 at 01:16

13 Answers13

251

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:
Link

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
digEmAll
  • 56,430
  • 9
  • 115
  • 140
109

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

Mujah Maskey
  • 8,654
  • 8
  • 40
  • 61
  • Awesome, lets me copy and paste the entire result straight into a web config if I'm manually adding assemblies. Thanks! – GJKH Aug 17 '15 at 14:06
  • Great answer, as you can get the version as well (which can be important for troubleshooting). – sbkrogers Jan 20 '19 at 19:14
52

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">
...
user2341923
  • 4,537
  • 6
  • 30
  • 44
14

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;
}
ge0rg
  • 1,816
  • 1
  • 26
  • 38
Adam Caviness
  • 3,424
  • 33
  • 37
  • I tried your code but it's not working. I want to get the public key token of Stimulsoft.Report.Web.dll which is already in my References. I copied your code and did pass the name of the dll but it's not working. My code goes something like this: string s = GetPublicKeyTokenFromAssembly("Stimulsoft.Report.Web") . Obviously the method is accepting assembly , not a string name. Can you help me how to use your method? – Francis Saul Mar 08 '16 at 13:49
  • @FrancisSaul What does the method return, "None" or an incorrect value? Have you determined that the assembly is indeed strong named? You can verify in VS on the properties panel for the reference (Strong Name). You can also easily check this with most free decompilers like JustDecompile. If you see it is strong named, debug the code and inspect the bytes. – Adam Caviness Mar 08 '16 at 19:30
  • I can't run the project, after I type the code string s = GetPublicKeyTokenFromAssembly("Stimulsoft.Report.Web"), it says the best overload for method has invalid arguments. – Francis Saul Mar 09 '16 at 01:52
  • @FrancisSaul You're passing a string argument to a method that takes an Assembly. You can `typeof(YourPreferredTypeInThatAssembly).Assembly` or `Assembly.GetAssembly(typeof(YourPreferredTypeInThatAssembly))` and pass that to the method if you like. – Adam Caviness Mar 09 '16 at 16:03
  • So the syntax would be like this. Assembly.GetAssembly(typeof("Stimulsoft.Report.Web.dll)); – Francis Saul Mar 10 '16 at 01:10
  • @FrancisSaul - No. You are passing in a *file* name. `YourPreferredTypeInThatAssembly` would be the name of some **type** in your assembly. For example, `typeof(System.Int32).Assembly` would return an Assembly object for Microsoft's `mscorlib`. For example, on my pc the object prints as "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089". `GetPublicKeyTokenFromAssembly(typeof(System.Int32).Assembly)` returns `"b77a5c561934e089"`. `typeof` uses the COMPILER to find the desired Type, then after that is loaded at RUNTIME, its assembly can be queried. – ToolmakerSteve Oct 11 '17 at 23:23
14

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);
            }
apoorv020
  • 5,420
  • 11
  • 40
  • 63
  • it's not getting the public key token of the dll's in my References. Is your method really gets all the dll in the references of a certain project? thanks – Francis Saul Mar 08 '16 at 13:51
  • @FrancisSaul - it will only list assemblies that have been LOADED into your application's process memory; to see details for a specific assembly, first run code that refers to some type in that assembly. For example, create a new instance of some class in that assembly. – ToolmakerSteve Oct 11 '17 at 23:12
  • 1
    This was the simplest method that worked for me. It can be simplified further: Run the application with a breakpoint set. When the execution pauses on the breakpoint, in the VS Immediate Window type: `AppDomain.CurrentDomain.GetAssemblies()`. This will list the loaded assemblies in the Immediate Window, with Version, Culture and PublicKeyToken for each. – Simon Elms Oct 17 '17 at 23:11
  • It also is possible to run in 'C# Interactive' - no compiling needed! – bTab Mar 27 '18 at 06:47
10

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.

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • 1
    When you select this from the `Tools` menu, it will automatically operate on whatever Project you have selected in Solution Explorer. – Contango Jun 17 '15 at 08:58
  • I renamed my command to `Get PublicKeyToken on Solution Explorer Selection`. – Contango Jun 17 '15 at 08:59
  • 1
    Good answer! **Hint:** If you enable the checkbox "Prompt for arguments", then a dialog opens up allowing you to paste a path into it. So you could copy+paste an assemly path from the properties window there for example. That was very useful for me, especially when I had to change registrations in the web.config and didn't know a DLLs public key token. – Matt Jul 28 '16 at 08:42
9

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

user2173353
  • 4,316
  • 4
  • 47
  • 79
8

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 .

Pawel Gorczynski
  • 1,227
  • 1
  • 15
  • 17
5

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

Syd
  • 1,526
  • 1
  • 15
  • 16
5

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

    string key = string.Join ("",assembly
                                .GetName()
                                .GetPublicKeyToken()
                                .Select (b => b.ToString ("x2")));
Yogi
  • 9,174
  • 2
  • 46
  • 61
3

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

Julian
  • 33,915
  • 22
  • 119
  • 174
1

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.

bhupendra patel
  • 3,139
  • 1
  • 25
  • 29
0

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

Kryszal
  • 1,663
  • 14
  • 20