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(); } } }
source share