Good evening. I would like to know if it is possible to include a file in a Visual Studio project programmatically at runtime?
Try it this way:
var p = new Microsoft.Build.Evaluation.Project(@"C:\MyFolder\MyProject.csproj");
string strNewItem = @"C:\MyImages\IMG_20180926-WA0017.jpg";
if (p.Items.FirstOrDefault(x => x.EvaluatedInclude == strNewItem) == null)
{
p.AddItem("_img", strNewItem);
p.Save();
}
Gets the project and then validates if the file you want to include does not already exist, in which case it adds it to the _img
folder and writes the project.
User-submitted Ray Carneiro .
Need to reference Microsoft.Build.dll
.