Visual Studio displaying errors even if projects b

2019-01-08 03:19发布

问题:

I have a problem with Visual Studio on a C# solution. It displays totally random errors, but the projects build. Right now, I have 33 files with errors, and I can see red squiggly lines in all of them.

I tried cleaning / rebuilding the solution, closing Visual Studio and even restarting my computer. I also made sure to do the steps described in Debugging runs even with compiler's errors in Visual Studio. I can modify .cs files and I see the changes in the solution.

Does anyone have an idea about why it does that?

回答1:

If you have ReSharper, try emptying the ReSharper cache:

In menu, ReSharper > Options > Environment > General > Clear Caches

and disabling and re-enabling ReSharper:

In menu, Tools > Options > ReSharper > General > Suspend / Restore



回答2:

Clearing Resharper's cache did not help in my case, tried suspend/restore, and also Repair Resharper, using latest download off JetBrains' website - neither of these helped. This is after I tried close/reopen VS, restart my machine, repeat, Build/Rebuild and combination thereof.

It's interesting that suspending Resharper seemed to solve the problem after the 2nd restart of VS, but it was back after I enabled Resharper <-- I tried to do this sequence 2-3 times to ensure the pattern.

Anyway, I was still having issues when I found this article:

  • Quick tip: What to do when Visual Studio freaks out and everything is red

So I deleted the hidden .SUO file on the same folder level with solution, and it magically solved all reds.

Note - for Visual Studio 2015, the .SUO file is in .vs/[solution_name]/v14 hidden folder.



回答3:

I cleaned solution, closed VS, reopened it, build solution, and red unresolved lines were cleaned and build succeeded.



回答4:

tldr; Unload and reload the problem project.

When this happens to me I (used to) try closing VS and reopen it. That probably worked about half of the time. When it didn't work I would close the solution, delete the .suo file (or the entire .vs folder) and re-open the solution. So far this has always worked for me (more than 10 times in the last 6 months), but it is slightly tedious because some things get reset such as your build mode, startup project, etc.

Since it's usually just one project that's having the problem, I just tried unloading that project and reloading it, and this worked. My sample size is only 1 but it's much faster than the other two options so perhaps worth the attempt. I suspect this works because it writes to the .suo file, and perhaps fixes the corrupted part of it that was causing the issue to begin with.

Note: I'm using Visual Studio 2015.



回答5:

I found that happens frequently when using Git in Visual Studio 2017, switching branches where there is dependent code changes. Even though the project will build successfully, there will remain errors in the error list.

These errors are often namespace issues and missing references, even when the library reference exists.

To resolve:

  • Close Visual Studio
  • Delete the {sln-root}.vs\SlnName\v15.suo file (hidden)
  • Restart Visual Studio


回答6:

I had a problem like this where Intellisense didn't seem to recognise the existence of one project (lots of "can't find this type", "this namespace doesn't exist", etc. errors).

Removing and re-adding the project reference in all the referencing projects would fix the issue, but the underlying cause could be fixed by editing the .proj file of the problem project.

Near the top of the "missing" project' .csproj file is an element:

<ProjectGuid>{GUID}</ProjectGuid>

and in all of the referencing projects .csproj files were project references:

<ProjectReference Include="..\OffendingProject\OffendingProject.csproj">
  <Project>{ANOTHER-GUID}</Project>
  <Name>Offending Project</Name>
</ProjectReference>

The referencing GUID didn't match the project's GUID. Replacing {GUID} above with {ANOTHER-GUID} fixed the problem without having to go through every referencing project.



回答7:

Occasionally I have to do a custom clean by going through all of the projects and manually deleting the "bin" and "obj" folders. To see them in Visual Studio, you'll have to enable hidden files and folders for each project. After this is done, rebuild the solution.



回答8:

I have tried all the 6 options, nothing worked for me. Below solution resolved my issue.

Close VS. Delete the hidden ".vs" folder next to your solution file. Restart VS and load the solution.



回答9:

Perhaps you try to reset your intellisense cache. I've had a similar issue in visual studio 2012 when working in a large project with many partial class definitions. Reducing the partials solved the problem partially, clearing the intellisense cache also - for a while.



回答10:

for VS-2017, deleting .vs folder worked for me.



回答11:

For my specific case it was a service reference another developer merged into the main branch. Which was perfectly fine except syntax highlight failed to resolve the generated service class and source was all red underlined. Cleaning, rebuilding, restarting did nothing.

All I had to do was refresh the service reference and VS managed to put the pieces together behind the scenes. No changes in the source code or generated files.



回答12:

I've just ran into this issue after reverting a git commit that added files back into my project.

Cleaning and rebuilding the project didn't work, even if I closed VS inbetween each step.

What eventually worked, was renaming the file to something else and changing it back again. :facepalm:



回答13:

After trying all of the options listed I discovered yet another reason why this can happen. If somebody sent you the source code as a zip, or you downloaded a zip, Windows may have blocked all files. 2 ways to solve this:

Method 1:

Right click on the original Zip file -> Check 'Unblock' -> Click apply

Method 2:

If that's not an option, rather than opening properties on every file in the solution folder simply open power shell and unblock recursively using the following:

Get-ChildItem -Path 'C:\<ROOT FOLDER OF SOLUTION>\' -Recurse | Unblock-File


回答14:

Following solution worked for me

1 - Close VS

2 - Delete .vs folder

3 - Open VS

4 - Build solution



回答15:

Sometimes if you just clean solution the errors are disappearing, but they may eventualy come back afer a while or at the next build.



回答16:

Ran into this issue with a single type not being recognized by Visual Studio which showed the red squiggle even though the solution built successfully. I noticed in the Solution Explorer the file did not have the expand arrow on the left which shows classes and properties on expansion.

The fix was to Exclude the file from the project and save/build which produced an expected error and then Include the file in the project and save and build.

After performing these steps Visual Studio started to recognize my type again. Looking at the diff in git it appears the issue was due to line endings not matching on the <Compile Include="..." /> line of my .csproj file.



回答17:

0 - Right click on the Solution and clean solution

1 - Close VS

2 - Delete project's .suo file

3 - Open VS

4 - Build solution



回答18:

in my case vs was never retaining the imported namespaces in the project properties > references

when I tried to add/check them again I couldn't and vs threw an error and when saved project vs crashed. When I reopened all the standard imported namespaces (system.data etc...) were all ticked again and it then was recognising everything without error



回答19:

  1. first close the solution.
  2. then solution cache file delete( in location C:\Users\Documents\Visual Studio\Backup Files/project cache file)
  3. then .suo file delete
  4. then solution open and build.

I hope solve your problem



回答20:

TL;DR: Perform a clean re-install of Visual Studio

After wasting several hours, I still couldn't fix it for Visual Studio 2017. I then installed Visual Studio 2019 PREVIEW and all of a sudden, IntelliSense displays me the members of STL-classes again (which it doesn't with Visual Studio 2017).

So, my guess is that there might be also something wrong with Visual Studio itself (maybe something in the cache directory or in general something on your PC which is not directly related to a specific solution) which can be solved by a clean and complete re-installation of Visual Studio. I know, this is a stupid "solution", but in my case, only a fresh Visual Studio (2019) installation had an effect.

As already mentioned, in my case, only STL classes were affected. IntelliSense wouldn't display their members which is odd. I thought, it could maybe have something to do with precompiled headers. Somewhere I read that the STL and the project should be on the same drive and putting them onto the same should solve the problem. But none of these routes led to success.



回答21:

Here's a collection of popular answers. Upvote the OP of the answer if it helped you:

Option 1: Clean, Build and Refresh (@Mike Fuchs option)

As @Mike Fuchs mentioned, try the following operations:

In menu, Build > Clean Solution

And

In menu, Build > Build Solution

and select the project in question, and click on the refresh button:

Option 2: Clean, Close, Restart and Build (@Pixel option)

As @Pixel mentioned, try the following sequence of operations:

  1. Clean the solution
  2. Close Visual Studio
  3. Open Visual Studio
  4. Build solution

Option 3: Clear ReSharper cache (@CydrickT option)

If you have ReSharper, try emptying the ReSharper cache:

In menu, ReSharper > Options > Environment > General > Clear Caches

and disabling and re-enabling ReSharper:

In menu, Tools > Options > ReSharper > General > Suspend / Restore

Option 4: Delete the .suo file (@Neolisk option)

As @Neolisk mentioned, deleting the .suo file might solve your problem. For Visual Studio 2015, the file is located in:

[Path of Solution]/.vs/[Solution Name]/v14/.suo

And for Visual Studio 2017:

[Path of Solution]/.vs/[Solution Name]/v15/.suo

Note that the .vs directory is hidden.

Option 5: Unload and Reload Project (@TTT option)

As @TTT mentioned, try unloading the project that causes problems:

In Solution Explorer, right-click on project, Unload Project.

And re-loading it

In Solution Explorer, right-click on project, Reload Project.

Option 6: Remove and add Microsoft.CSharp reference (@Guilherme option)

As @Guilherme mentioned, try removing and adding the reference to "Microsoft.CSharp" from the projects that have problems.

In Solution Explorer, expand the project, expand "References", right-click on "Microsoft.CSharp" and Remove.

Then, right-click on References > Add Reference, select "Microsoft.CSharp" from the list and click OK



回答22:

Try hovering with the mouse over the underlined elements. It should normally tell you what the problem. To see a list of all the errors/warnings, go to View => Error List. A table should open on the bottom of the IDE with all the errors/warnings listed.



回答23:

I solved this problem by deleting the temporary files of Microsoft .NET framework. Location: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files and C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files



回答24:

REM DELETE ALL VS HIDDEN SOLUTION OPTION FILES
DEL /A:H /S *.SUO