Modify .csproj files programmatically
Asked Answered
R

2

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?

Ricci answered 25/10, 2011 at 9:55 Comment(1)
Why not write a C# program that uses the XDocument class?Mayhem
C
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.

Crosspurpose answered 13/12, 2011 at 16:26 Comment(7)
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.FrameworkCrosspurpose
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.Bus
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.Crosspurpose
How you made the changes? is it possible using powershell ? Is required Microsoft.Build.Engine and Microsoft.Build.Framework references ?Ricci
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.Crosspurpose
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.Gyral
Microsoft.Build.Engine isnt working and Microsoft.Build.Evaluation is not there. Any other library to set up Engine and Project objec ?Nab
B
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
Backward answered 24/1, 2023 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.