none
loading projects for manipulation RRS feed

  • Question

  • Hi,

    I currently have a need to programmatically add an existing file to a project.  To do this, I have referenced Microsoft.Build (4.0) from my 4.5 project.  I then use the following code to load the project:

    var project = new Project("<path to csproj file>");


    When I run it I'm getting the following error:

    System.TypeInitializationException: The type initializer for 'SelfServiceNuGetInstaller.SolutionHelper' threw an 
    exception. ---> Microsoft.Build.Exceptions.InvalidProjectFileException: The imported project "C:\Program Files 
    (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm 
    that the path in the <Import> declaration is correct, and that the file exists on disk.

    I should note that I am using VS13 and do not have any other version installed. I believe the Microsoft.Build DLL is looking for an older version of VS that I don't have installed.  I  might be able to correct this by locating and updating an Environment Variable, but I plan on running this code in a nuget package so I don't want the user to have to do that.

    Does anyone know if there is a way to tell the Build DLL where the correct VisualStudio "vX" folder is?

    TIA,

    Matt



    There is indeed a harmony to the universe.

    Thursday, July 3, 2014 4:28 PM

Answers

  • Thank you for the reply.  I had found that thread. Unfortunately, it seems most project modification examples on the web assume you are creating an Add-In, which I'm not.  I did take my code and test it in a console app and it worked. Now I just need to figure out why it isn't working in my nuget install.

    For anyone interested, here is the working code to add an existing item to a project:

    using Microsoft.Build.Evaluation;
    
    var project = new Project(@"C:\project\TestProject\ConsoleApp1.csproj");
    var items = project.AddItem("Content", @"C:\project\TestProject\test.txt");
    project.Save();


    There is indeed a harmony to the universe.

    • Marked as answer by Mulhearn22 Thursday, July 10, 2014 1:11 PM
    Thursday, July 10, 2014 1:11 PM

All replies

  • Hi Mulhearn,

    >Does anyone know if there is a way to tell the Build DLL where the correct VisualStudio "vX" folder is?

    Per my understanding, this question is related to MS build. I’m not familiar with it. You should open thread on that forum. http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=msbuild.

    For the error message “Microsoft.Build.Exceptions.InvalidProjectFileException”, it indicates there is a problem with the user’s XML project file. I followed the page below to build a project to test add item to project. It worked. Please try it. http://msdn.microsoft.com/en-us/library/vstudio/ms228774(v=vs.110).aspx.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 4, 2014 4:23 AM
    Moderator
  • Thank you for the reply.  I had found that thread. Unfortunately, it seems most project modification examples on the web assume you are creating an Add-In, which I'm not.  I did take my code and test it in a console app and it worked. Now I just need to figure out why it isn't working in my nuget install.

    For anyone interested, here is the working code to add an existing item to a project:

    using Microsoft.Build.Evaluation;
    
    var project = new Project(@"C:\project\TestProject\ConsoleApp1.csproj");
    var items = project.AddItem("Content", @"C:\project\TestProject\test.txt");
    project.Save();


    There is indeed a harmony to the universe.

    • Marked as answer by Mulhearn22 Thursday, July 10, 2014 1:11 PM
    Thursday, July 10, 2014 1:11 PM