195

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.

2

13 Answers 13

255

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

5
  • 1
    Yeah i know that, I'm looking for an easier way, preferably without leaving VS
    – moi_meme
    Jun 15, 2010 at 12:53
  • 12
    Replace %ProgramFiles% with %ProgramFiles(x86)% if you're on a 64-bit machine. Apr 23, 2014 at 22:38
  • 1
    For VS2012 it is under "%ProgramFiles(x86)%\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\sn.exe" -T <assemblyname>. Note sn is tucked in one extra folder level after bin (in NETFX 4.0 Tools).
    – GDS
    Jun 16, 2016 at 23:22
  • 2
    Nice elaborated answer. For VS2015 it should be under %ProgramFiles(x86)%\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\sn.exe Oct 13, 2016 at 18:51
  • 1
    VS2015 path should be v10.0A, not V1.0A Feb 1, 2017 at 21:06
111

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

2
  • Awesome, lets me copy and paste the entire result straight into a web config if I'm manually adding assemblies. Thanks!
    – GJKH
    Aug 17, 2015 at 14:06
  • Great answer, as you can get the version as well (which can be important for troubleshooting).
    – sbkrogers
    Jan 20, 2019 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">
...
3
  • 3
    I see only <Reference Include="System.IdentityModel" /> (same for other asseblies)
    – FrenkyB
    May 11, 2016 at 8:42
  • Doesn't seem to be much use for assemblies in the .NET system libraries. eg for System.Configuration it just says <Reference Include="System.Configuration" />
    – Simon Elms
    Oct 17, 2017 at 23:04
  • It looks like all assemblies have the same PublicKeyToken, so one look up in the .cproj seems to be the easiest solution.
    – asdf
    Jan 9, 2019 at 10:34
15

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;
}
6
  • 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? Mar 8, 2016 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. Mar 8, 2016 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. Mar 9, 2016 at 1: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. Mar 9, 2016 at 16:03
  • So the syntax would be like this. Assembly.GetAssembly(typeof("Stimulsoft.Report.Web.dll)); Mar 10, 2016 at 1:10
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);
            }
4
  • 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 Mar 8, 2016 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. Oct 11, 2017 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, 2017 at 23:11
  • It also is possible to run in 'C# Interactive' - no compiling needed!
    – bTab
    Mar 27, 2018 at 6: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.

3
  • 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, 2015 at 8:58
  • I renamed my command to Get PublicKeyToken on Solution Explorer Selection.
    – Contango
    Jun 17, 2015 at 8: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, 2016 at 8: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! ;))

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 .

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

1
  • Yeah i know that, I'm looking for an easier way, preferably without leaving VS
    – moi_meme
    Jun 15, 2010 at 14:17
5

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

    string key = string.Join ("",assembly
                                .GetName()
                                .GetPublicKeyToken()
                                .Select (b => b.ToString ("x2")));
1
  • 1
    He asked for the public key token. Your code uses the public key. It should be .GetPublicKeyToken() Oct 30, 2018 at 10:19
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

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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.