'A reference to the dll could not be added. The component is Automatically Referenced C#

I am getting an error in C# Visual Studio and there doesn't seem to be any clear-to-the-point answer to this question online.

When I want to add mscorlib, I get this error message:

A reference to 'mscorlib' could not be added. This component is automatically referenced by the project system and cannot be referenced directly.

but .EnumerateFiles() needs that library.

Why would this message show when one want to add a specific reference?



Solution 1:[1]

MSDN is good place to solve such mysteries -DirectoryInfo.EnumerateFiles clearly shows that it is supported by 4.0 and 4.5 versions of .NET.

You are trying to link against 2.0 (or maybe something like 3.0/3.5) and do not have such function. Your error clearly says that you can't add the assembly to the project since it is always referenced automatically for correct version of runtime.

To be able to use the new EnumerateFile, you need to build against 4.0+, or write code without that function.

Solution 2:[2]

EnumerateFiles require .NET 4 or later. I think you are building your solution targetting .NET Framework 3.5 or earlier.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CarenRose
Solution 2 Saurabh R S