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
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"
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.
Source: Stackoverflow.com