Keywords: .NET | PublicKeyToken | Assembly
Abstract: This article provides an in-depth exploration of various methods to obtain the PublicKeyToken of .NET assemblies. It begins with a detailed explanation of using the sn.exe command-line tool, covering path configurations for different Visual Studio versions and operating system architectures. Alternative approaches via PowerShell reflection API and project file inspection are also discussed. Through code examples and step-by-step instructions, the article systematically explains the applicability and considerations of each method, offering developers a comprehensive technical reference.
Introduction
In .NET development, the PublicKeyToken is a critical component of strongly named assemblies, serving to uniquely identify the publisher. Retrieving the PublicKeyToken is essential for scenarios such as assembly referencing, version control, and security validation. However, the Visual Studio IDE does not provide a direct graphical interface for this operation, which can pose challenges for developers. This article systematically introduces multiple methods to obtain the PublicKeyToken, with a focus on the most commonly used command-line tool approach, supplemented by other practical techniques.
Using the sn.exe Command-Line Tool
The sn.exe (Strong Name Tool) provided by Microsoft is the standard utility for retrieving PublicKeyTokens. This tool is located in the Windows SDK directories, with specific paths varying based on Visual Studio version and operating system architecture.
Path Configuration Details
For Visual Studio 2010, on 32-bit Windows systems, the tool path is: "%ProgramFiles%\Microsoft SDKs\Windows\v7.0A\bin\sn.exe". On 64-bit Windows systems, access to the 32-bit program files directory is required, so the path should be: "%ProgramFiles(x86)%\Microsoft SDKs\Windows\v7.0A\bin\sn.exe". It is important to note that starting from Visual Studio 2012, sn.exe was moved to subdirectories, such as \NETFX 4.0 Tools\ for VS2012.
Basic Usage
The -T parameter can be used to quickly obtain the PublicKeyToken. The basic command format is: sn.exe -T <assemblyname>. Here, <assemblyname> should be replaced with the full file path to the target assembly. If the path contains spaces, it must be enclosed in quotes. For example: sn.exe -T "C:\Program Files\MyAssembly.dll". Upon execution, the tool outputs a result similar to Public key token is 1234567890abcdef.
Integration into Development Environment
To enhance productivity, this command can be configured as an external tool in Visual Studio. The steps involve opening Visual Studio, navigating to the "Tools" menu, selecting "External Tools," adding a new tool, and setting the command to the path of sn.exe with arguments such as -T "$(TargetPath)". This allows direct operation on the output assembly of the current project.
PowerShell Reflection Method
In addition to command-line tools, PowerShell can be used to retrieve PublicKeyTokens via .NET reflection APIs. This method is particularly suitable for automation scripts or scenarios requiring programmatic access.
A basic code example is: [system.reflection.assembly]::loadfile("full path to dll").FullName. Executing this command outputs the full name of the assembly, which includes the PublicKeyToken. For instance: MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d. This approach leverages the LoadFile method of the System.Reflection.Assembly class to load the assembly, then uses the FullName property to obtain the complete identifier including the PublicKeyToken.
Inspecting Project Files
For assembly references already added to a Visual Studio project, the PublicKeyToken can be directly viewed in the project file (e.g., .csproj). The <Reference> elements in the project file typically contain complete assembly information.
For example: <Reference Include="Microsoft.Dynamic, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">. This method is applicable for quickly checking project dependencies but is limited to explicitly referenced assemblies.
Method Comparison and Selection Recommendations
Comparing the methods discussed, the sn.exe command-line tool is the most versatile and reliable option, applicable to all strongly named assemblies and independent of specific development environments. The PowerShell method offers programming flexibility, ideal for integration into automated workflows. Project file inspection is the quickest approach but has limited applicability. Developers should choose the appropriate method based on specific needs, such as prioritizing PowerShell scripts in continuous integration environments, while using command-line tools or Visual Studio external tool integrations for daily development.
Conclusion
Retrieving the PublicKeyToken of .NET assemblies is a common requirement in development. This article has detailed three primary methods. By effectively applying these techniques, developers can efficiently handle tasks related to assembly identification, ensuring proper referencing and security in projects. It is recommended to select the most suitable method based on specific scenarios and consider integrating common operations into development tools to enhance productivity.