Freigeben über


Visual Studio Tip: Get Public Key Token for a Strong Named Assembly

I can't remember where I picked this tip up from, but I have found it useful on many occasions so I thought that I would share it.

Sometimes I need to reference the strong named assembly that I am writing in a config file or some other location, and I need to put in the fully qualified name such as:

 MyNamespace.MyAssembly, version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

The first 3 parts are easy to get. I should know the name, version, and culture for the assembly since I am writing it. The part that can be a little harder to locate is the Public Key Token for my signed assembly. One common way to do this is to use Reflector to open my assembly and get the token (actually, Reflector will give you the entire fully qualified name as in the example above).  For me, that is just too much work.  If I have the project in Visual Studio already, I would much rather just click a menu item in Visual Studio to get the result.  Here is how that can be set up:

In Visual Studio, go to the Tools menu and click the External Tools menu item. This will bring up the External Tools dialog.  The image below shows the information that I have added to add a new menu item called 'Get SN Token'.

External Tools Dialog 

The command is the path to sn.exe which can be in different places depending on your VS version.  The easiest way to find it is to open a VS Command Prompt and type "where sn.exe".  The arguments field is set to -T and then the $(TargetPath) variable.  The "Use Output Window" option is checked so that the results will be shown in the VS output window.  After clicking OK, this will be enabled as a menu item as shown below.

VS Tools Menu 

The output for this command will be displayed in the output window. This also works if you have multiple projects in the same solution. 
Just highlight the project in Solution Explorer and then click the menu
item.

Output Window

Comments

  • Anonymous
    February 19, 2008
    Jeremiah Clarke has an awesome post about how to set up Visual Studio to retrieve the public key for

  • Anonymous
    February 21, 2008
    Great tip! Thanks heaps

  • Anonymous
    April 30, 2008
    This is the very definition of sublime <grin>!  Thanks.

  • Anonymous
    June 09, 2008
    Hey, Jerimiah. Just wanted to thank you for the tip... I'd still be looking for the PublicKeyToken had I not stumbled across your post...

  • Anonymous
    July 31, 2008
    Very helpful and easy to follow, it took me 1min to find out my PublicKeyToken. Thanks

  • Anonymous
    August 25, 2008
    Really you saved me lot of time Many thanks

  • Anonymous
    August 26, 2008
    I couldn't find the Get SN Token on External Tool. What's VS version do you demo?

  • Anonymous
    August 26, 2008
    BMW,  This post shows how you can add the ability to get the token to the list of external tools.  It is not built-in to any version of VS. Jeremiah

  • Anonymous
    September 08, 2008
    Having some probs in VS2005, with the instruction    open a VS Command Prompt and type "where sn.exe".... I assume this refers to the VS Command Window, but when I type it in, I get:     Command "where" is not valid. Any thoughts / help Cheers

  • Anonymous
    September 08, 2008
    Ok - cracked it

  • I was wrong in my assumption about "VS Command Window"
  • it was exactly as it stated " VS Command Prompt"    - I found this from the Windows Start Menu, via Visual Studio / Visual Studio Tools
  • Anonymous
    September 16, 2008
    Great tip, elegant solution... Thanks!

  • Anonymous
    October 28, 2008
    Well done! This trick will save me time!

  • Anonymous
    October 29, 2008
    Cool trick one. Thanks for your sharing

  • Anonymous
    February 25, 2009
    This is awesome and is really helpful.  Thanks!

  • Anonymous
    March 17, 2009
    This guy is a genus. Why the in world MS did not include this I have know idea.

  • Anonymous
    April 14, 2009
    Thanks for documenting this trick; big time saver!

  • Anonymous
    May 29, 2009
    This is also nice if you use the -Tp parameter -- it'll show you the public key as well as the key token, like so;    Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.30729.1    Copyright (c) Microsoft Corporation.  All rights reserved.    Public key is    00240003048000009404000006020000002400055253413100040000710001000f507849113404    5955a3f8fdc1bd0d29cba6357026e7caf1631831c64d71fc09051a29444d1d5b4199331d6a1c9d    883de7837dd553b26f82d9bfacad5e405a286fad65cd3e2a890925314e6d34dd3102448cd8a7c8    f16cd1b39b180b34985faa799f3d21e5c81f86467b5f02f451cc3473858d1e7bef63ee39440edf    b64ef8a8    Public key token is 74786c738e63f883

  • Anonymous
    September 29, 2009
    I've used: -T $(ItemPath) This way I can select the library I want to :-) I hope it is util for yours ;)

  • Anonymous
    October 01, 2009
    Very useful and time saving. thanks.

  • Anonymous
    November 04, 2009
    Thanks for this post. It was a great help to me.

  • Anonymous
    November 06, 2009
    Great tip! This will save me loads of time.

  • Anonymous
    March 23, 2010
    Just want to say a quick thank you. Your tip worked perfectly for me in VS08.

  • Anonymous
    May 10, 2010
    Don't know how you figure this stuff out but thanks for saving me a bunch of time!

  • Anonymous
    July 09, 2010
    The comment has been removed

  • Anonymous
    November 05, 2010
    I bow to your greatness - Thanks

  • Anonymous
    November 18, 2010
    Program Files (x86)Microsoft SDKsWindowsv7.0Abinsn.exe Thanks!

  • Anonymous
    January 09, 2011
    The comment has been removed

  • Anonymous
    January 16, 2011
    Worked great in Visual Studio 2005. Thanks!

  • Anonymous
    January 24, 2011
    Thanks a lot!

  • Anonymous
    March 25, 2011
    Windows Server 2003 Enterprice Edition (x86) + Visual Studio 2008 Path:  C:Program FilesMicrosoft SDKsWindowsv6.0ABinsn.exe

  • Anonymous
    August 08, 2011
    Thanks for the tip! :)

  • Anonymous
    August 18, 2011
    The comment has been removed

  • Anonymous
    January 11, 2012
    Brilliant Tip, thanks.

  • Anonymous
    March 03, 2012
    AWESOME! MANY THANKS!!!

  • Anonymous
    March 13, 2012
    THANKS ALLOT.

  • Anonymous
    May 14, 2012
    Great work!!!

  • Anonymous
    June 17, 2012
    Great tip

  • Anonymous
    September 17, 2012
    The comment has been removed

  • Anonymous
    January 09, 2013
    how do i get the token of my file of my chosing?

  • Anonymous
    February 25, 2013
    You can also use a UI based tool from assemblyinformation.codeplex.com

  • Anonymous
    July 16, 2013
    nice post..But I could not find sn.exe in bin folder...help me..!!!

  • Anonymous
    July 16, 2013
    The comment has been removed

  • Anonymous
    July 22, 2013
    very good and thanks good luck

  • Anonymous
    October 16, 2013
    Thank you. I needed this information.

  • Anonymous
    September 21, 2014
    click the link and start an online job to earn by doing simple task www.probux.com

  • Anonymous
    September 25, 2014
    Super.    I modified the args slightly as shown: -T $(BinDir)$(CurText).dll So in my web.config, if the assy is copied locally to bin, I can just highlight the assy name and this will work for any assy in the bin dir, not just the project target.

  • Anonymous
    October 28, 2014
    thank you! Very useful post!

  • Anonymous
    August 07, 2015
    Very Sweet!  Works like a charm.