Condividi tramite


How to: Add or Remove References By Using the Add Reference Dialog Box

In order to use a component in your application, you must first add a reference to it. Visual Studio provides five options in the Add Reference dialog box:

  • .NET lists all .NET Framework components available for referencing.

  • COM lists all COM components available for referencing.

  • Projects lists all reusable components created from local projects.

  • Browse enables you to browse for a component in the file system.

  • Recent contains a list of components recently added to projects on your computer.

The number of tabs available at the top of the Add Reference dialog box can vary, depending on the type of project open and the resources it is using. C++ native projects contain only a Projects tab.

Some components in the list may not be shown, depending on the .NET Framework version of your project. This can occur under the following conditions:

  • A component that uses a recent version of the .NET Framework is incompatible with a project that targets an earlier version of the .NET Framework.

    For information about how to change the target .NET Framework version for a project, see How to: Target a Version of the .NET Framework.

  • A component that uses .NET Framework 4 is incompatible with a project that targets the .NET Framework 4 Client Profile.

    When you create a new application, some projects target the .NET Framework 4 Client Profile by default. For more information, see .NET Framework Client Profile.

You cannot add references from the global assembly cache because it is strictly part of the run-time environment.

If you deploy or copy an application that contains a reference to a custom component registered in the global assembly cache, the component will not be deployed or copied with the application, regardless of the Copy Local setting. For more information, see Managing Project References.

You can create applications that reference projects or assemblies that target a different version of the .NET Framework. For example, you could create an application that targets the .NET Framework 4 Client Profile that references an assembly that targets .NET Framework 2.0. If you create a project that targets an earlier version of the .NET Framework, you cannot set a reference in that project to a project or assembly that targets the .NET Framework 4 Client Profile or .NET Framework version 4. For more information, see Targeting a Specific .NET Framework Version or Profile.

You should avoid adding file references to outputs of another project in the same solution, because doing this may cause compilation errors. Instead, use the Projects tab of the Add Reference dialog box to create project-to-project references. This makes team development easier by enabling better management of the class libraries you create in your projects. For more information, see Troubleshooting Broken References.

Note

In Visual Studio 2010, a file reference instead of a project reference is created if the target version of the .NET Framework of one project is version 4, and the target version of the other project is version 2, 3, or 3.5.

If you want to add a reference to a registered COM DLL that contains an internal manifest, make sure that you unregister the DLL first. If you do not, Visual Studio adds the assembly reference as an ActiveX Component instead of as a native DLL.

You can also add Web references by using the Add Web Reference dialog box. For more information, see How to: Add and Remove Web References.

When you manually add a reference to any of the EnvDTE namespaces (EnvDTE, EnvDTE80, EnvDTE90, EnvDTE90a, or EnvDTE100), set the Embed Interop Types property of the reference to False in the Properties window. Setting this property to True can cause build issues because of certain EnvDTE properties that cannot be embedded.

Note

All projects contain an implied reference to mscorlib. Visual Basic projects contain an implied reference to Microsoft.VisualBasic.

In Visual Studio 2010, all projects contain an implied reference to System.Core, even if System.Core is removed from the list of references.

To display an assembly in the Add Reference dialog box

  • Move or copy the assembly to one of the following locations:

    • The current project directory. (You can find these assemblies by using the Browse tab.)

    • Other project directories in the same solution. (You can find these assemblies by using the Projects tab.)

    - or -

  • Set a registry key that specifies the location of assemblies to display:

    For a 32-bit operating system, add one of the following registry keys.

    • [HKEY_CURRENT_USER\SOFTWARE\Microsoft\.NETFramework\VersionMinimum\AssemblyFoldersEx\MyAssemblies]@="AssemblyLocation"

    • [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\VersionMinimum\AssemblyFoldersEx\MyAssemblies]@="AssemblyLocation"

    For a 64-bit operating system, add one of the following registry keys in a 32-bit registry hive.

    • [HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\VersionMinimum\AssemblyFoldersEx\MyAssemblies]@="AssemblyLocation"

    • [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\VersionMinimum\AssemblyFoldersEx\MyAssemblies]@="AssemblyLocation"

    VersionMinimum is the lowest .NET Framework version that applies. If VersionMinimum is v3.0, folders specified in AssemblyFoldersEx apply to projects that target .NET Framework 3.0 and later.

    AssemblyLocation is the directory of the assemblies that you want to appear in the Add Reference dialog box, for example, C:\MyAssemblies\.

    Creating the registry key under the HKEY_LOCAL_MACHINE node allows all users to see the assemblies in the specified location in the Add Reference dialog box. Creating the registry key under the HKEY_CURRENT_USER node affects only the setting for the current user.

    Open the Add Reference dialog box again. The assemblies should appear on the .NET tab. If they do not, ensure that the assemblies are located in the specified AssemblyLocation directory, restart Visual Studio, and try again.

To add a reference in Visual Basic

  1. In Solution Explorer, double-click the My Project node for the project.

  2. In the Project Designer, click the References tab.

  3. Click the Add button to open the Add Reference dialog box.

  4. In the Add Reference dialog box, select the tab indicating the type of component you want to reference.

  5. Select the components you want to reference, and then click OK.

    Tip

    You can select multiple components by holding down the CTRL key, provided the components are all on the same tab.

To add a reference in Visual C#

  1. In Solution Explorer, right-click the project node and click Add Reference.

  2. In the Add Reference dialog box, select the tab indicating the type of component you want to reference.

  3. Select the components you want to reference, and then click OK.

    Tip

    You can select multiple components by holding down the CTRL key, provided the components are all on the same tab.

To remove a reference in Visual Basic

  1. In Solution Explorer, double-click the My Project node for the project.

  2. In the Project Designer, click the References tab.

  3. In the References list, select the reference you want to remove.

  4. Click the Remove button.

    Tip

    You can also check your project for unused references and remove all of them at one time. For more information, see How to: Remove Unused References (Visual Basic).

To remove a reference in Visual C#

  1. In Solution Explorer, open the References node under the project node.

  2. Right-click a reference and click Remove.

See Also

Tasks

Troubleshooting Broken References

How to: Add or Remove Imported Namespaces (Visual Basic)

Reference

Application Page, Project Designer (C#)

Advanced Compiler Settings Dialog Box (Visual Basic)

Concepts

Managing Project References

How to: Add or Remove References By Using the Reference Manager

Managing Project References

Visual Studio Multi-Targeting Overview

Troubleshooting .NET Framework Targeting Errors

Other Resources

Targeting a Specific .NET Framework Version or Profile