How to programmatically include a file in my project?

纵饮孤独 提交于 2019-11-27 12:02:57

It worked for my just adding the it to the ProjectFolder, and also add the folder programmatically like this.

var p = new Microsoft.Build.Evaluation.Project(@"C:\projects\BabDb\test\test.csproj");
        p.AddItem("Folder", @"C:\projects\BabDb\test\test2");
        p.AddItem("Compile", @"C:\projects\BabDb\test\test2\Class1.cs");
        p.Save();

As a supplement to Boot750's answer (first suggested as an edit to his answer but re-posted as a standalone)

You should note that the call to new Microsoft.Build.Evaluation.Project(path) actually causes the project to be loaded into a global cache maintained by the Microsoft.Build.Evaluation assembly. Calling new Project(path) again with the same path, without unloading it from the global collection first/restarting your app will cause an exception like:

An equivalent project (a project with the same global properties and tools version) is already present in the project collection, with the path "YOUR_PATH". To load an equivalent into this project collection, unload this project first.

even if the variable p has gone out of scope.

You might hence need to adopt a pattern more like this, if you plan to use the Load/AddItem/Save pattern repeatedly:

 var p =
Microsoft.Build.Evaluation
  .ProjectCollection.GlobalProjectCollection
    .LoadedProjects.FirstOrDefault(pr => pr.FullPath == projFilePath);

if (p == null)
   p = new Microsoft.Build.Evaluation.Project(projFilePath);
rerun

The simplest way to do this is to modify the Project file. As it is just an MSBUILD file, VS will pick up the change and prompt up to reload the project. Load the project file as an XML and find the first <Compile Include="Name of File.cs" />. Insert a new <Compile Include="NewFile.CS" /> and your done.

As another option you can remove all the <Compile> tags and replace them with <Compile Include="*.cs" />

Just to add to the response from @caius-jard

Once the project is in the GlobalProjectCollection it's held in memory and does not reflect any manual changes made to the project. This includes updating the .csproj file and using VS to remove the generated files.

If you remove the generated file and run the code again the project will update to contain 2 of the generated file. Use the ReevaluateIfNecessary() function to update the instance of the project before use like this:

var p = Microsoft.Build.Evaluation
        .ProjectCollection.GlobalProjectCollection
        .LoadedProjects.FirstOrDefault(pr => pr.FullPath == projFilePath);

if (p == null)
    p = new Microsoft.Build.Evaluation.Project(projFilePath);

// Update instance of project
p.ReevaluateIfNecessary();

// Check folder is not already in the project
var folderLoc = @"C:\projects\BabDb\test\test2";
if(p.Items.FirstOrDefault(i => i.EvaluatedInclude == folderLoc) == null)
    p.AddItem("Folder", folderLoc);

// Check file is not already in the project
var fileLoc = @"C:\projects\BabDb\test\test2\Class1.cs";
if(p.Items.FirstOrDefault(i => i.EvaluatedInclude == fileLoc) == null)
    p.AddItem("Compile", fileLoc);

p.Save();

You could use T4 Text Templates. See http://msdn.microsoft.com/en-us/library/vstudio/bb126445(v=vs.100).aspx for more information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!