The naïve solution would be to edit the XML file manually. However, you don't have to do this.
You can use the Microsoft.Build.Evaluation.Project object model to manipulate Visual Studio project files. See this answer for more information on the differences between the Microsoft.Build.Evaluation
and Microsoft.Build.Construction
namespaces.
The following example opens YourProject.csproj
and adds a new file called YourFile.cs
as a Compile
item type, then saves the project file back 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();
}
}
}