1

I built a Web Forms website using VS2015 where I user Microsoft.Build.Evaluation so that I can grammatically go through the files in my project. When using VS2017 I get this error:

Microsoft.Build.Exceptions.InvalidProjectFileException: 'The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk. C:\Users\MyUser\Source\Workspaces\MyProject TFVC2\Gemstar\MyProject.csproj'

Here is my code:

using Microsoft.Build.Evaluation;

Project project = new Project();
if (ProjectCollection.GlobalProjectCollection.GetLoadedProjects(mPath + "MyProject.csproj").Count == 0)
{
    project = new Project(mPath + "MyProject.csproj");
}
else
{
    project = ProjectCollection.GlobalProjectCollection.GetLoadedProjects(mPath + "MyProject.csproj").FirstOrDefault();
}

I want to mention that I installed BuildTools for VS2017 from https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
boruchsiper
  • 2,016
  • 9
  • 29
  • 53
  • 1
    Did you confirm the `Microsoft.WebApplication.targets` file exists on disk? – NightOwl888 Feb 14 '18 at 02:09
  • Yes. In fact it works fine vs 2015 – boruchsiper Feb 14 '18 at 02:11
  • @boruchsiper, `When using VS2017 I get this error`--Have you installed VS2017 on your build server? If yes, why you installed build tools? Besides, the import path for `Microsoft.WebApplication.targets` should be incorrect. It exists in the path `C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0` or `C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\VisualStudio\v15.0`. – Leo Liu Feb 14 '18 at 02:24

3 Answers3

2

Mac OS -
I resolved it by installing the latest version of Mono.

  1. Installing Mono on macOS.
  2. Search "omnisharp" in settings (command + ',').
  3. Changed the setting Omnisharp: Use Global Mono to always.
  4. Reload the vscode.
Gaurav Sharma
  • 573
  • 1
  • 4
  • 26
1

Can't use Microsoft.Build.Evaluation in VS2017

It seems the old value of MSBuildExtensionsPath32 set in the Microsoft.Build.Evaluation.

According to the error info:

The imported project "C:\Program Files(x86)\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" was not found.

The import path for Microsoft.WebApplication.targets is not correct.

In the project file .csproj, we notice following import:

  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />

So the value for MSBuildExtensionsPath32 is C:\Program Files (x86)\MSBuild in the Microsoft.Build.Evaluation, which is not correct for Visual Studio 2017.

The value of MSBuildExtensionsPath32 for Visual Studio 2017 should be:

BuildTool Installed:

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0

Visual Studio Installed:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\VisualStudio\v15.0

To resolve this issue, you could verride the value in your project file:

<PropertyGroup>
    <MSBuildExtensionsPath32>C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0</MSBuildExtensionsPath32>
  </PropertyGroup>

  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />

See the similar issue for some more details.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
0

Install Wix Toolset Visual Studio 2017 Extension.

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148