Dela via


How to: Add or Remove References in Visual Studio (Visual Basic)

Note

The procedure for setting the reference path for Visual C# projects is slightly different. In a Visual Basic project, click the Reference Paths button to display the Add Reference dialog box. In a Visual C# project, use the Reference Paths Page, Project Designer (C#) of the Project Designer. For more information, see How to: Set the Reference Path (C#).

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 Add Reference dialog box lists only assemblies in the Public Assemblies folder (Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies). You can copy your own assemblies to the Public Assemblies folder, or browse to them directly. Note that you cannot add references from the Global Assembly Cache (GAC), as it is strictly part of the run-time environment.

Note

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. Also, some components in the list may be disabled and display in gray depending on the .NET Framework version of your project. This is because some components from a more recent .NET Framework version are not compatible with projects that are based on an earlier .NET Framework version.

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

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.

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.

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.)

    • The Public Assemblies folder: Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies. (You can find these assemblies on the .NET tab.)

    - or -

  • Set a reference path to the directory that contains the assembly by using the Reference Paths Dialog Box (Visual Basic) or the Reference Paths Page, Project Designer (C#).

    - 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 location 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 folder, 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.

    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 set a reference path 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 Reference Paths button.

  4. In the Reference Paths dialog box, in the Folder field, enter the full path of the folder that contains the components.

  5. Click the Add Folder button, and then click OK.

    Note

    Only the specified folder will be searched every time that the Add Reference dialog box is opened; subfolders will not be searched. You must add a separate path for each subdirectory to be searched.

To set a reference path in Visual C#

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

  2. In the Project Designer, click the Reference Paths tab.

  3. In the Folder field, enter the full path of the folder that contains the components.

  4. Click the Add Folder button, and then click OK.

    Note

    Only the specified folder will be searched every time that the Add Reference dialog box is opened; subfolders will not be searched. You must add a separate path for each subdirectory to be searched.

See Also

Tasks

Troubleshooting Broken References

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

How to: Remove Unused References (Visual Basic)

How to: Set the Copy Local Property of a Reference

How to: Add New Project Items

How to: Add and Remove Web References

Concepts

Project References

Web References in Visual Studio

Other Resources

Managing References

Referencing Namespaces and Components