Inclusion of files in Visual Studio projects

Asked

Viewed 204 times

0

Good evening. I wonder if it is possible to include a file in a Visual Studio project by programming at runtime ?

inserir a descrição da imagem aqui

1 answer

0


Try doing 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(); 
}

Get the project and then validate if the file you want to include does not yet exist, in which case you add it to the folder _img and save the project.

Indication given by the user Ray Carneiro.

Need to reference Microsoft.Build.dll.

  • Thank you João Martins

  • Edited response to contain the library needed for the code to run.

  • Did the answer help you? Did you solve the problem?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.