获取.Net程序集的PublicKeyToken

发布于 2024-09-06 08:57:24 字数 137 浏览 9 评论 0原文

查找程序集的公钥令牌的最简单方法是什么?

我能想到的最简单的方法是简单的右键单击,获取公钥,但这个功能不存在,也许有一个 Visual Studio 扩展?

我正在使用 Visual Studio 2010(如果有可用的扩展)。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

慈悲佛祖 2024-09-13 08:57:24

打开命令提示符,然后根据您的 Visual Studio 版本和操作系统体系结构键入以下行之一:

32 位 Windows 上的 VS 2008 :

"%ProgramFiles%\Microsoft SDKs\Windows\v6.0A\bin\sn.exe" -T <assemblyname>

64 位 Windows 上的 VS 2008 :

"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\bin\sn.exe" -T <assemblyname>

32 位 Windows 上的 VS 2010:

"%ProgramFiles%\Microsoft SDKs\Windows\v7.0A\bin\sn.exe" -T <assemblyname>

64 位 Windows 上的 VS 2010:

"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v7.0A\bin\sn.exe" -T <assemblyname>

32 位 Windows 上的 VS 2012:

"%ProgramFiles%\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\sn.exe" -T <assemblyname>

64 位 Windows 上的 VS 2012:

"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\sn.exe" -T <assemblyname>

64 位 Windows 上的 VS 2015:

"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\sn.exe" -T <assemblyname>

请注意,对于 VS2012+ 版本,sn.exe 应用程序不再位于 bin 中,而是位于子文件夹中。另请注意,对于 64 位,您需要指定 (x86) 文件夹。

如果您更喜欢使用 Visual Studio 命令提示符,只需键入:

sn -T <assembly> 

其中 < assemblyname> 是您感兴趣的程序集的完整文件路径,如果包含空格,则用引号引起来。

您可以将其添加为 VS 中的外部工具,如下所示:
相对于="noreferrer">链接

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

只想待在家 2024-09-13 08:57:24

另一种选择:

如果您使用 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

并将显示为

MySql.Data,版本=6.6.5.0,文化=中性,
PublicKeyToken=c5687fc88969c44d

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

乖乖 2024-09-13 08:57:24

如果该库包含在VS项目中,您可以检查.cproj文件,例如:

<ItemGroup>
    <Reference Include="Microsoft.Dynamic, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
...

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">
...
云裳 2024-09-13 08:57:24

您可以通过 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 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;
}
╰沐子 2024-09-13 08:57:24

如果您已将程序集包含在项目中,您可以执行以下操作:

            var assemblies =
                AppDomain.CurrentDomain.GetAssemblies();


            foreach (var assem in assemblies)
            {
                    Console.WriteLine(assem.FullName);
            }

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);
            }
机场等船 2024-09-13 08:57:24

您可以将其作为外部工具添加到 Visual Studio,如下所示:

标题:

Get PublicKeyToken

命令:

c:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\sn.exe

(版本之间的路径可能有所不同)

参数:

-T "$(TargetPath)"

并选中“使用输出窗口”选项。

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.

勿挽旧人 2024-09-13 08:57:24

对我来说最简单的方法是使用 ILSpy

当您拖动 &将程序集拖放到其窗口上,然后选择左侧拖放的程序集,您可以在窗口右侧看到公钥令牌。

(我还认为,如果您需要的话,较新的版本还将显示签名的公钥......请参见此处:https://github.com/icsharpcode/ILSpy/issues/610#issuecomment-111189234。好东西!;))

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

Hello爱情风 2024-09-13 08:57:24

如果有人正在寻找程序集公钥(像我一样),而不是公钥令牌 - 使用 sn.exe 效果很好,除非您必须使用 -Tp 开关,它将返回公钥和公钥令牌 - 更多信息请参见 https://msdn.microsoft.com/en-us/library/office/ee539398(v=office.14).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 .

〆一缕阳光ご 2024-09-13 08:57:24

1) 命令为 C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\sn -T {your.dll}

在上面的示例中,Microsoft SDK 位于 C:\ Program Files\Microsoft SDKs\Windows\v6.0A。您的环境可能有所不同。

2) 要获取任何项目的公钥令牌,您可以将 sn.exe 添加为 Visual Studio 中的外部工具的一部分。这些步骤显示在 此 Microsoft 链接:如何:创建一个工具来获取公众信息程序集的关键

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

木格 2024-09-13 08:57:24

作为替代方案,您还可以像这样使用 linq -

    string key = string.Join ("",assembly
                                .GetName()
                                .GetPublicKeyToken()
                                .Select (b => b.ToString ("x2")));

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

    string key = string.Join ("",assembly
                                .GetName()
                                .GetPublicKeyToken()
                                .Select (b => b.ToString ("x2")));
把梦留给海 2024-09-13 08:57:24

另一种选择是使用开源工具 NuGet Package Explorer 来实现此目的。

从 Nuget 包 (.nupkg) 中,您可以检查 PublicKeyToken,或将二进制文件 (.dll) 拖到工具中。对于后者,首先选择“文件 -> 新建”

在此处输入图像描述

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

音盲 2024-09-13 08:57:24

另一种方法是,如果您有反编译器,只需在那里查找即可,它们通常会提供公钥。我看过 .Net Reflector、Telerik Just Decompile 和 ILSpy just decompile 他们似乎显示了公钥令牌。

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.

鲜血染红嫁衣 2024-09-13 08:57:24

您可以使用 Ildasm.exe(IL 反汇编程序)检查程序集的元数据,其中包含完全限定名称。

遵循MSDN:
https://msdn.microsoft.com/en-我们/library/2exyydhb(v=vs.110).aspx

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文