The imported project "C:\Microsoft.CSharp.targets" was not found

C#Visual Studio

C# Problem Overview


I got this error today when trying to open a Visual Studio 2008 project in Visual Studio 2005:

> The imported project "C:\Microsoft.CSharp.targets" was not found.

C# Solutions


Solution 1 - C#

Open your csproj file in notepad (or notepad++) Find the line:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

and change it to

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

Solution 2 - C#

> This is a global solution, not dependent on particular package or bin.

In my case, I removed Packages folder from my root directory.

> Maybe it happens because of your packages are there but compiler is not finding it's reference. so remove older packages first and add new packages.

Steps to Add new packages

  • First remove, packages folder (it will be near by or one step up to your current project folder).
  • Then restart the project or solution.
  • Now, Rebuild solution file.
  • Project will get new references from nuGet package manager. And your issue will be resolved.

This is not proper solution, but I posted it here because I face same issue.

In my case, I wasn't even able to open my solution in visual studio and didn't get any help with other SO answers.

Solution 3 - C#

For me the issue was that the path of the project contained %20 characters, because git added those instead of spaces when the repository was cloned. Another problem might be if the path to a package is too long.

Solution 4 - C#

In my case I could not load one out of 5 projects in my solution.

It helped to close Visual Studio and I had to delete Microsoft.Net.Compilers.1.3.2 nuget folder under packages folder.

Afterwards, open your solution again and the project loaded as expected

Just to be sure, close all instances of VS before you delete the folder.

Solution 5 - C#

This link on MSDN also helps a lot to understand the reason why it doesn't work. $(MSBuildToolsPath) is the path to Microsoft.Build.Engine v3.5 (inserted automatically in a project file when you create in VS2008). If you try to build your project for .Net 2.0, be sure that you changed this path to $(MSBuildBinPath) which is the path to Microsoft.Build.Engine v2.0.

Solution 6 - C#

I used to have this following line in the csproj file:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />

After deleting this file, it works fine.

Solution 7 - C#

If you are to encounter the error that says Microsoft.CSharp.Core.targets not found, these are the steps I took to correct mine:

  1. Open any previous working projects folder and navigate to the link showed in the error, that is Projects/(working project name)/packages/Microsoft.Net.Compilers.1.3.2/tools/ and search for Microsoft.CSharp.Core.targets file.

  2. Copy this file and put it in the non-working project tools folder (that is, navigating to the tools folder in the non-working project as shown above)

  3. Now close your project (if it was open) and reopen it.

It should be working now.

Also, to make sure everything is working properly in your now open Visual Studio Project, Go to Tools > NuGetPackage Manager > Manage NuGet Packages For Solution. Here, you might find an error that says, CodeAnalysis.dll is being used by another application.

Again, go to the tools folder, find the specified file and delete it. Come back to Manage NuGet Packages For Solution. You will find a link that will ask you to Reload, click it and everything gets re-installed.

Your project should be working properly now.

Solution 8 - C#

I got this after reinstalling Windows. Visual Studio was installed, and I could see the Silverlight project type in the New Project window, but opening one didn't work. The solution was simple: I had to install the Silverlight Developer runtime and/or the Microsoft Silverlight 4 Tools for Visual Studio. This may seem stupid, but I overlooked it because I thought it should work, as the Silverlight project type was available.

Solution 9 - C#

In my case, I opened my .csproj file in notepad and removed the following three lines. Worked like a charm:

<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />
<Import Project="..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" />

Solution 10 - C#

> ok so what if it say this: between the > gt/lt signs > > Import > Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight\v3.0\Microsoft.Silverlight.CSharp.targets" > / > > how do i fix the targets error?

I also found that import string in a demo project (specifically "Build your own MVVM Framework" by Rob Eisenburg).

If you replace that import with the one suggested by lomaxx VS2010 RTM reports that you need to install this.

Solution 11 - C#

For errors with Microsoft.WebApplications.targets, you can:

  1. Install Visual Studio 2010 (or the same version as in development machine) in your TFS server.
  2. Copy the “Microsoft.WebApplication.targets” from development machine file to TFS build machine.

Here's the post.

Solution 12 - C#

This error can also occur when opening a Silverlight project that was built in SL 4, while you have SL 5 installed.

Here is an example error message: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Silverlight.CSharp.targets" was not found.

Note the v4.0.

To resolve, edit the project and find:

<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>

And change it to v5.0.

Then reload project and it will open (unless you do not have SL 5 installed).

Solution 13 - C#

I deleted the obj folder and then the project loaded as expected.

Solution 14 - C#

Sometimes the problem might be with hardcoded VS version in .csproj file. If you have in your csproj something like this:

[...]\VisualStudio\v12.0\WebApplications\Microsoft.WebApplication.targets"

You should check if the number is correct (the reason it's wrong can be the project was created with another version of Visual Studio). If it's wrong, replace it with your current version of build tools OR use the VS variable:

[...]\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets"

Solution 15 - C#

I ran into this issue while executing an Ansible playbook so I want to add my 2 cents here. I noticed a warning message about missing Visual Studio 14. Visual Studio version 14 was released in 2015 and the solution to my problem was installing Visual Studio 2015 Professional on the host machine of my Azure DevOps agent.

Solution 16 - C#

After trying to restore, closing VS, deleting the failed package, reopening, trying to restore, multiple times I just deleted everything in packages and when I did a restore and it worked perfectly.

Solution 17 - C#

For me, the issue was the path.. When cloning the project that had a space in the name. The project folder was named "Sample%20-%205" instead of what it should be: "Sample - 5"

Opening the project was fine, but building failed with

> Could not find the file: > /packages/Microsoft.Net.Compilers.1.3.2/tools/Microsoft.CSharp.Core.targets

Solution 18 - C#

it seems now that the nuget packages folder has moved to a machine wide global cache, using VS2022

visual studio 2022 global nuget packages

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlomaxxView Question on Stackoverflow
Solution 1 - C#lomaxxView Answer on Stackoverflow
Solution 2 - C#BharatView Answer on Stackoverflow
Solution 3 - C#user2042930View Answer on Stackoverflow
Solution 4 - C#VojtaView Answer on Stackoverflow
Solution 5 - C#Oleg SakharovView Answer on Stackoverflow
Solution 6 - C#appenthusedView Answer on Stackoverflow
Solution 7 - C#Alf MohView Answer on Stackoverflow
Solution 8 - C#PeterView Answer on Stackoverflow
Solution 9 - C#GichambaView Answer on Stackoverflow
Solution 10 - C#Andre LuusView Answer on Stackoverflow
Solution 11 - C#stack247View Answer on Stackoverflow
Solution 12 - C#Greg GumView Answer on Stackoverflow
Solution 13 - C#Atron SeigeView Answer on Stackoverflow
Solution 14 - C#WholeLifeLearnerView Answer on Stackoverflow
Solution 15 - C#AlexView Answer on Stackoverflow
Solution 16 - C#Worthy7View Answer on Stackoverflow
Solution 17 - C#AndreasView Answer on Stackoverflow
Solution 18 - C#Mark HomerView Answer on Stackoverflow