How to: Create a Tool to Get the Public Key of an Assembly
Last modified: January 28, 2011
Applies to: SharePoint Foundation 2010
SharePoint Foundation development projects often involve a mixture of imperative coding and XML markup. Frequently, you add your new assembly's Public Key Token to a project file, such as an XML configuration file. If your Visual Studio project is based on any of the SharePoint 2010 project templates in Visual Studio, you can simply insert the placeholder $SharePoint.Project.AssemblyPublicKeyToken$ where the Public Key Token should be in most kinds of project files (but not .cs or .vb files or other compilable files). When you build and deploy the project, Visual Studio will replace the placeholder with the Public Key Token in the copies of the files that are actually deployed. (For more information about the use of SharePoint-oriented Visual Studio placeholders, see Replaceable Parameters.)
If you need to insert the Public Key Token in a file that does not support the placeholders, you will need a way to obtain the Public Key Token. This topic explains how to add an item to the Visual StudioTools menu that can be used to obtain the Public Key Token of an assembly under development.
To add a Get Assembly Public Key item to the Tools menu
In Visual Studio, click External Tools on the Tools menu.
In the External Tools dialog box, click Add and enter Get Assembly Public Key in the Title box.
Fill the Command box by browsing to sn.exe. It is typically installed at the following location: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0a\Bin\x64\sn.exe.
In the Arguments box, type the following (case sensitive): -Tp $(TargetPath).
Select the Use Output window check box.
Click OK. The new command is added to the Tools menu.
Whenever you need the Public Key Token of the assembly you are developing, click the Get Assembly Public Key command on the Tools menu, and the public key token appears in the Output window.
Because the Public Key Token does not exist until the assembly is compiled the first time, you will need to compile the project before using the tool.