获取 .Net 程序集的 PublicKeyToken
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3045033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the PublicKeyToken of .Net assemblies
提问by moi_meme
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?
我能想到的最简单的方法是简单的右键单击,获取公钥,但是这个功能不存在,也许有一个 Visual Studio 扩展?
I'm using Visual Studio 2010, if an extension is available.
我正在使用 Visual Studio 2010,如果有扩展可用。
回答by digEmAll
Open a command prompt and type one of the following lines according to your Visual Studio version and Operating System Architecture :
打开命令提示符并根据您的 Visual Studio 版本和操作系统架构键入以下行之一:
VS 2008 on 32bit Windows:
VS 2008 在 32 位 Windows 上:
"%ProgramFiles%\Microsoft SDKs\Windows\v6.0A\bin\sn.exe" -T <assemblyname>
VS 2008 on 64bit Windows :
VS 2008 在 64 位 Windows 上:
"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\bin\sn.exe" -T <assemblyname>
VS 2010 on 32bit Windows :
VS 2010 在 32 位 Windows 上:
"%ProgramFiles%\Microsoft SDKs\Windows\v7.0A\bin\sn.exe" -T <assemblyname>
VS 2010 on 64bit Windows :
64 位 Windows 上的 VS 2010:
"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v7.0A\bin\sn.exe" -T <assemblyname>
VS 2012 on 32bit Windows :
VS 2012 在 32 位 Windows 上:
"%ProgramFiles%\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\sn.exe" -T <assemblyname>
VS 2012 on 64bit Windows :
64 位 Windows 上的 VS 2012:
"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\sn.exe" -T <assemblyname>
VS 2015 on 64bit Windows :
64 位 Windows 上的 VS 2015:
"%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.
请注意,对于 VS2012+ 版本,sn.exe 应用程序不再位于 bin 中,而是位于子文件夹中。另请注意,对于 64 位,您需要指定 (x86) 文件夹。
If you prefer to use Visual Studio command prompt, just type :
如果您更喜欢使用 Visual Studio 命令提示符,只需键入:
sn -T <assembly>
where <assemblyname>is a full file path to the assembly you're interested in, surrounded by quotes if it has spaces.
where<assemblyname>是您感兴趣的程序集的完整文件路径,如果有空格,则用引号括起来。
You can add this as an external tool in VS, as shown here:
http://blogs.msdn.com/b/miah/archive/2008/02/19/visual-studio-tip-get-public-key-token-for-a-stong-named-assembly.aspx
您可以将其添加为 VS 中的外部工具,如下所示:http:
//blogs.msdn.com/b/miah/archive/2008/02/19/visual-studio-tip-get-public-key-token -for-a-stong-named-assembly.aspx
回答by Mujah Maskey
another option:
另外一个选项:
if you use PowerShell, you can find out like:
如果你使用 PowerShell,你会发现:
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
MySql.Data,版本=6.6.5.0,文化=中性,PublicKeyToken=c5687fc88969c44d
回答by user2341923
If the library is included in the VS project, you can check .cprojfile, e.g.:
如果库包含在 VS 项目中,您可以检查.cproj文件,例如:
<ItemGroup>
<Reference Include="Microsoft.Dynamic, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
...
回答by apoorv020
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);
}
回答by Adam Caviness
You can get this easily via c#
您可以通过 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;
}
回答by Danny Varod
You can add this as an external tool to Visual Studio like so:
您可以将其作为外部工具添加到 Visual Studio,如下所示:
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.
并选中“使用输出窗口”选项。
回答by user2173353
The simplest way for me is to use ILSpy.
对我来说最简单的方法是使用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! ;))
(我还认为较新的版本也会显示签名的公钥,如果您需要的话……请参见此处:https: //github.com/icsharpcode/ILSpy/issues/610#issuecomment-111189234。好东西! ;))
回答by Yogi
As an alternative, you can also use linqlike this -
作为替代方案,您也可以linq像这样使用-
string key = string.Join ("",assembly
.GetName()
.GetPublicKeyToken()
.Select (b => b.ToString ("x2")));
回答by Pawel Gorczynski
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 -Tpswitch, 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.
如果有人正在寻找程序集公钥(像我一样),而不是公钥令牌- 使用 sn.exe 效果很好,除非您必须使用-Tp开关,它将返回公钥和公钥令牌 - 更多在https://msdn.microsoft.com/en-us/library/office/ee539398(v=office.14).aspx。
回答by Syd
1) The command is C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\sn -T {your.dll}
1)命令是 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.
在上面的示例中,Microsoft SDK 位于 C:\Program Files\Microsoft SDKs\Windows\v6.0A。您的环境可能有所不同。
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
2) 要获取任何项目的公钥令牌,您可以在 Visual Studio 中将 sn.exe 添加为外部工具的一部分。此 Microsoft 链接中显示了这些步骤:如何:创建获取程序集公钥的工具

