web site counter


How to give an assembly a strong name

It's not hard, but as I had to learn how to do this yesterday while creating a sharepoint webpart, I thought it might be helpful to post the steps up here.

  1. Open the Visual Studio.NET Command Prompt
  2. Type:

    • sn.exe -k <path for key file to be saved>\<name of key file>.snk

      • I'd recommend saving the key file in the same directory as the project
      • I'd also recommend giving the key file the same name as the assembly (with out the extension)

  3. The key file will now have been created, you should see a message like: Key pair written to <path for key file to be saved>\<name of key file>.snk
  4. Open the AssemblyInfo.cs file in your project
  5. Change the [assembly: AssemblyVersion("1.0.*")] to a "hard" version, such as [assembly: AssemblyVersion("")].  This is so any references to the assemblys strong name (which contains the name, version and public key), don't break when the project is rebuilt and the version is automatically incremented.
  6. Change the [assembly: AssemblyKeyFile("")] to be [assembly: AssemblyKeyFile("<path for key file to be saved>\<name of key file>.snk")]
  7. Rebuild
  8. Done

To find out the public key token for your assembly, type the following into your Visual Studio.NET Command Prompt:

sn.exe -T <path to assembly>\<assembly name>

You should see a message like:

Public key token is c9d6fafd5a623323

So now when you want to refer to your assembly by it's strong name, you'll use something like:

<assembly name>, Version=, Culture=neutral, PublicKeyToken=c9d6fafd5a623323