10

I have source code of Entlib 5.0 and I need sign all assemblies using my own key (snk file).

The easiest way would be to open the EnterpriseLibrary.2010 solution file in Visual Studio 2010 and for each project, select Properties->Signing then select Sign the Assembly and finally select your key file.

But I don't want to manually do that then I could write a script to manually edit the project files and insert the following at the end of the current list of PropertyGroups:

<PropertyGroup>
    <SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
    <AssemblyOriginatorKeyFile>keyFile.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

Any helper class in C# or scripting if were better for do it easy and quick way?

Stphane
  • 3,368
  • 5
  • 32
  • 47
Kiquenet
  • 14,494
  • 35
  • 148
  • 243

2 Answers2

24

You can take a look at the Microsoft.Build.BuildEngine namespace MSDN Link

Sample code:

Engine eng = new Engine()
Project proj = new Project(eng);
proj.Load(FullProjectPath);
proj.SetProperty("SignAssembly", "true");
proj.Save(FullProjectPath);

I recently used something similar to make a series of changes across all of my company's .csproj files (over 200) instead of manually opening each project and making changes.

Hope this helps.

Tyson Moncrief
  • 526
  • 3
  • 10
  • 1
    sure, when I used it, I wrote a console application that looked at our source directory and picked up all the .csproj files and then made the changes I needed. You just need to add a reference to Microsoft.Build.Engine and Microsoft.Build.Framework – Tyson Moncrief Dec 15 '11 at 19:24
  • Do you know off hand if the "Post Build Event" portion of the csproj is available via this library? I've fished a little bit. But haven't found it yet. Thanks. And "Upvote" for this response. – granadaCoder Oct 10 '12 at 21:32
  • sure, you should be able to do something like Target postBuild = proj.Targets["AfterBuild"]; assuming that it was un-commented in the project file. I poked around for about 5 seconds and didn't see how to add a Target if it didnt exist, but I know it can be done. Also, this namespace is obsolete and you may have more luck with its replacement. – Tyson Moncrief Oct 17 '12 at 22:05
  • How you made the changes? is it possible using `powershell` ? Is **required** _Microsoft.Build.Engine and Microsoft.Build.Framework_ references ? – Kiquenet Oct 01 '15 at 17:06
  • I wrote my application in C#. I have not used powershell before but if I am not mistaken, you can reference any of the C# libraries as part of your script. The Microsoft.Build.Engine and Microsoft.Build.Framework are the two assemblies that contain the Engine and Project object used for manipulating the csproj files so those references are required. – Tyson Moncrief Oct 19 '15 at 16:33
  • 1
    Engine is deprecated and people should use Microsoft.Build.Evaluation...the only issue with Evaluation is the target framework. If you are using .netstandard it will target an older framework which can throw errors. The only solution is to use an xml reader and writer. – NewBie1234 Mar 09 '21 at 14:56
  • 1
    Microsoft.Build.Engine isnt working and Microsoft.Build.Evaluation is not there. Any other library to set up Engine and Project objec ? –  Jun 06 '21 at 19:29
0

As some have noted the other answer that Engine is now deprecated and people should use Microsoft.Build.Evaluation however Evaluation if you are using .netstandard it will target an older framework which can throw errors.

An another option is to call this dotnet tool:

https://www.nuget.org/packages/dotnet-property

dotnet property "**/Project.csproj" AssemblyOriginatorKeyFile:"keyFile.snk"

A final option would be to do this at build time (without a tool)

dotnet build /p:AssemblyOriginatorKeyFile=keyFile.snk
John
  • 29,788
  • 18
  • 89
  • 130