简体   繁体   中英

Visual Studio cannot find any references for a new project

This is a weird one.

I have Visual Studio 2013 installed. If I create a new project, Visual Studio immediately complains that it cannot find any of the references:

The project does however compile and run correctly, even though there are warnings in the error list:

If I look at the properties of any of these references, their paths are empty. If I load an existing project, Visual Studio can find its references and doesn't complain, even though it is targeting the same version of the .Net framework as the broken project (I've even had them both in the same solution with the same behaviour).

How can I fix this, and what could have caused it? Any suggestions welcome, as it is baffling me.

With thanks to icemanind, I have the answer.

I closed Visual Studio, deleted the following directories and everything is now working as normal again:

C:\Users\<username>\AppData\Local\Microsoft\VisualStudio\12.0
C:\Users\<username>\AppData\Roaming\Microsoft\VisualStudio\12.0

I faced this problem, and I solved it by closing visual studio, reopening visual studio, cleaning and rebuilding the solution. This worked for me. On some other posts, I have read the replies and most of users solved the problem by following this way. Another possibility is that the target .NET Framework version of the class library is higher than that of the project.

This happens to me from time to time and I always suspect VS corruption, but it always turns out to be user error. It typically happens when I add a new project to a solution and try to reference earlier projects in the newly added project. In this case and in general, you can only add a reference if it targets the same or earlier .NET Framework version. In my case, inconsistent target framework versions caused the project to not recognize references and not allow compilation. Double checking consistency or proper target framework versions solved this problem for me.

I had a problem with an existing project, and by creating a new one and comparing the csproj files I had to add missing import lines like those: one at the beginning of the project element and one at the end.

    <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

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

I recently had this happen in VS 2017. I had done some refactoring on a project where the csproj file had moved, then cloned the repository onto a new machine. The csProj file had multiple locations for the .projects directory. For example:

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

There were additional relative path errors below in the Error tags as well.
I looked for any places where I had duplicated, incorrect package relative paths, and removed the incorrect ones.

This caused it to build for me.

Good luck out there! -Mezz

I had a similar issue with vs2015. It turns out a file was added twice to the .csproj

Deleting the duplicated line solved it

In a most cases you need:

  1. Unload Project
  2. Reload Project with dependecies
  3. Rebuild solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM