Add files programmatically to VS Project for UnitTests

I create class files programmatically for UnitTests using NUnit. I could already add them to TFS Source Control. What I need now is that I want to add them programmatically to the project.

I found several approaches, but it didn’t work for me ... I don’t want to manually edit the XMl file.

Do you have any code snippets?

+4
source share
1 answer

A naive solution would be to edit the XML file manually. However, you do not have to do this.

You can use the Microsoft.Build.Evaluation.Project object model to manage Visual Studio project files. See this answer for more information on the differences between Microsoft.Build.Evaluation and Microsoft.Build.Construction namespaces.

The following example opens YourProject.csproj and adds a new file called YourFile.cs as the type of the Compile element, and then saves the project file to disk.

 using Microsoft.Build.Evaluation; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var project = new Project(@"YourProject.csproj"); project.AddItem("Compile", "YourFile.cs"); project.Save(); } } } 
+5
source

Source: https://habr.com/ru/post/1444555/


All Articles