How to link a single key file to each project in a solution?

Viewed 751

I want to strong-name a few projects in a solution and have created a PFX key file to do it. I would like to have only one copy of this file for easy management, so I added it as a solution item. However, when I try to link the file in a project through the Properties->Signing menu in Visual Studio, it creates a local copy of the key file.

What I would like to be able to do, is to reference the existing key file in the solution and sign each project with it, without copies of the key file being created.

Is there a way to do this?

2 Answers

Use the 'add as link' option on the 'add existing item' dialog. This comes up a lot in BizTalk projects, since BizTalk assemblies must be signed, and often foxes beginners...it's a very well-hidden option.

enter image description here

Remove any existing reference to the file from your project(s). In Solution Explorer, right click the project, go to Add, Existing Item. Browse to the key file, and select it in the Add Existing Item dialog. In the bottom right of the Add Existing Item dialog, change the Add button to Add as Link by using the drop down, and then click the Add as Link button. The key file has now been added to your project as a link and not a copy.

In your project's properties you can now set the strong name key file to the file you have linked. Don't worry if this initially shows the absolute path to the file - when you save/re-open the project the absolute path will be changed to a relative path.

If you open the csproj file in a text editor, you will see something like this:

<None Include="..\..\Keys\MyKeyFile.snk">
  <Link>MyKeyFile.snk</Link>
</None>

If you have a lot of projects to change, you can do a global search and replace on the csproj files to directly change the item to a linked item.

Related