How do I find the PublicKeyToken for a particular dll?
Asked Answered
S

14

272

I need to recreate a provider in my web.config file that looks something like this:

<membership defaultProvider="AspNetSqlMemProvider">
  <providers>
    <clear/>
    <add connectionStringName="TRAQDBConnectionString" applicationName="TRAQ" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="0"
         name="AspNetSqlMemProvider"
         type="System.Web.Security.SqlMembershipProvider, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"
    />
  </providers>
</membership>

However, I get a runtime error saying this assembly cannot be loaded, and I think it is because I have the wrong PublicKeyToken. How do I look up the PublicKeyToken for my assembly?

Alternatively, am I going entirely the wrong way with this?

Suborn answered 10/11, 2009 at 20:23 Comment(1)
Possible duplicate of Getting the PublicKeyToken of .Net assembliesBozarth
L
426

Using PowerShell, you can execute this statement:

([system.reflection.assembly]::loadfile("C:\..\Full_Path\..\MyDLL.dll")).FullName

The output will provide the Version, Culture and PublicKeyToken as shown below:

MyDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a
Lodovico answered 10/6, 2014 at 8:17 Comment(14)
Thanks! your method is the only one that worked for me sn -T dllname.dll would only show help text when I ran itPermit
That's perfect! Avoids installing extra tools.Ripping
This is a way more easy to do it.Lemons
great! this works also when there is no PublicKeyToken available (ie. unsigned assemblies)Downstate
This is a more complete answer and covers more use cases. I needed it to refer to the the dll full name in code. Thanks!Brutish
In C# Interactive, you can call: Console.WriteLine(System.Reflection.Assembly.LoadFile(@"c:\some.dll").FullName);Spade
Exception calling "LoadFile" with "1" argument(s): "This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. (Exception from HRESULT: 0x8013101B)"Brumaire
Works like a charm!Weakwilled
Annoyingly, PowerShell.exe keeps a lock on the dll even after the call completed. I needed to close the PowerShell console before I could build my solution again.Aria
I don't know why PowerShell does not do any action when I call this commandAilina
My assembly has added to GAC, can I use just name?Decoder
Warning: You have to provide the full path. E.g. ([system.reflection.assembly]::loadfile("C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\cf562e7e\7984a186\assembly\dl3\1e53bd22\00cb376e_eed8cf01\System.Web.Mvc.dll")).FullNameVenison
Having to load a DLL to view its publickeytoken sounds risky. Is it possible to see the public key token via Nuget.org for example for the .NET ers out there ?Perilymph
You have to use the correct version of Powershell (32bit or 64bit) based on the architecture of your dll. Otherwise you get an error Exception calling "LoadFile" with "1" argument(s): "Could not load file or assembly 'Test.dll' or one of its dependencies. is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)".Quinidine
B
168

Using sn.exe utility:

sn -T YourAssembly.dll

or loading the assembly in Reflector.

Blackbird answered 10/11, 2009 at 20:27 Comment(2)
sn.exe can typically be found at one of the following locations under C:\Program Files (x86)\Microsoft SDKs\Windows: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin, C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64, C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 ToolsStimulative
Mind it, the key is case-sensitive. -t (lower case) will give the "Failed to convert key to token -- Invalid assembly public key.", which will send you searching in the wrong direction.Marmalade
C
42

If you have the DLL added to your project, you can open the csproj file and see the Reference tag.

Example:

<Reference Include="System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
Cowage answered 26/4, 2017 at 13:16 Comment(0)
G
26

sn -T <assembly> in Visual Studio command line. If an assembly is installed in the global assembly cache, it's easier to go to C:\Windows\assembly and find it in the list of GAC assemblies.

On your specific case, you might be mixing type full name with assembly reference, you might want to take a look at MSDN.

Gefen answered 10/11, 2009 at 20:26 Comment(0)
H
13

Answer is very simple use the .NET Framework tools sn.exe. So open the Visual Studio 2008 Command Prompt and then point to the dll’s folder you want to get the public key,

Use the following command,

sn –T myDLL.dll

This will give you the public key token. Remember one thing this only works if the assembly has to be strongly signed.

Example

C:\WINNT\Microsoft.NET\Framework\v3.5>sn -T EdmGen.exe

Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.21022.8
Copyright (c) Microsoft Corporation.  All rights reserved.

Public key token is b77a5c561934e089
Hooker answered 2/7, 2013 at 11:16 Comment(2)
If you have Visual Studio 2013 and you can't find "Developer Command Prompt for VS2013" go have a look here how to fix it: https://mcmap.net/q/81922/-where-is-developer-command-prompt-for-vs2013Tolbooth
Note that this tool does not come with Windows.Venison
E
5

I use Windows Explorer, navigate to C:\Windows\assembly , find the one I need. From the Properties you can copy the PublicKeyToken.

This doesn't rely on Visual Studio or any other utilities being installed.

Embolden answered 4/11, 2014 at 4:7 Comment(0)
H
3

Just adding more info, I wasn't able to find sn.exe utility in the mentioned locations, in my case it was in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin

Havener answered 15/10, 2014 at 15:1 Comment(2)
If you open up a visual studio command prompt it should be on your path.Tallman
Check this out: #3045533Optical
H
3

Assembly.LoadFile(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\system.data.dll").FullName

Will result in

System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Harilda answered 26/2, 2018 at 11:26 Comment(0)
S
3

If you want the token for something published on NuGet,

For example, OxyPlot.Wpf :

https://nuget.info/packages/OxyPlot.Wpf/2.1.0

and browse for the dll and its details.

Just change the nuget pkg name and version on the url for any other package.

Signore answered 17/5, 2022 at 8:14 Comment(0)
I
2

The top answer from danielB works, but PowerShell will put a lock on the file until you close PowerShell completely.

An alternative is to read in the bytes of the .dll and use load() instead of loadfile():

([system.reflection.assembly]::load([System.IO.File]::ReadAllBytes("C:\..\Full_Path\..\MyDLL.dll"))).FullName
Illfavored answered 3/10, 2023 at 15:40 Comment(0)
V
1

As @CRice said you can use the below method to get a list of dependent assembly with publicKeyToken

public static int DependencyInfo(string args) 
{
    Console.WriteLine(Assembly.LoadFile(args).FullName);
    Console.WriteLine(Assembly.LoadFile(args).GetCustomAttributes(typeof(System.Runtime.Versioning.TargetFrameworkAttribute), false).SingleOrDefault());
    try {
        var assemblies = Assembly.LoadFile(args).GetReferencedAssemblies(); 

        if (assemblies.GetLength(0) > 0)
        {
            foreach (var assembly in assemblies)
            {
                Console.WriteLine(" - " + assembly.FullName + ", ProcessorArchitecture=" + assembly.ProcessorArchitecture);             
            }
            return 0;
        }
    }
    catch(Exception e) {
        Console.WriteLine("An exception occurred: {0}", e.Message);
        return 1;
    } 
    finally{}

    return 1;
}

i generally use it as a LinqPad script you can call it as

DependencyInfo("@c:\MyAssembly.dll"); from the code

Valentinevalentino answered 1/12, 2018 at 13:39 Comment(0)
M
1

put into file dll-assemblyinfo in your $PATH:

#!/bin/sh

f=$(readlink -f "$1")

{
echo "using System.Reflection;"
echo "Assembly.LoadFile(\"$f\");"
} | csharp

chmod +x then

$ dll-assemblyinfo packages/System.Buffers.4.5.1/lib/netstandard2.0/System.Buffers.dll
System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
Menderes answered 31/5, 2022 at 15:0 Comment(3)
I'd love to use this! How do I find or install csharp?Sanchez
sudo apt install mono-csharp-shell ### see also mono-project.com/docs/tools+libraries/tools/replMenderes
Oh! Got it. Yeah, I thought you were using MSYS2 (or WSL) on Windows, and that csharp had been installed somewhere by the Visual Studio installer. Context is so important! :-DSanchez
K
0

You can also check by following method.

Go to Run : type the path of DLL for which you need public key. You will find 2 files : 1. __AssemblyInfo_.ini 2. DLL file

Open this __AssemblyInfo_.ini file in notepad , here you can see Public Key Token.

Katelin answered 21/4, 2014 at 7:50 Comment(0)
H
0

For DLL generated by MSVC or others
Using pktextract to get publicKeyToken from '.cer'
https://learn.microsoft.com/en-us/windows/win32/sbscs/pktextract-exe

See details from other answer: https://mcmap.net/q/110620/-how-to-view-public-key-tokens-on-dll-39-s

Heptangular answered 10/5, 2022 at 17:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.