How to: Manipulate the Imports Property of Visual Basic Projects
Most VSProject2 methods and properties apply to Visual C# and Visual Basic projects. For more information, see How to: Manipulate Visual Basic and C# Projects By Using the VSProject2 Object. The Imports property of the VSProject2 object is specific to Visual Basic projects. It provides access to the Imports object with methods for adding and enumerating the Imports collection.
The steps below explain how to programmatically control the Imports property in a Visual Basic project by using a Visual Studio add-in.
Poznámka
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.
To use the VSProject2 object to control Visual Basic projects
Create a Visual Studio Add-in project by using Visual C#.
On the Project menu, click Add Reference, click the .NET tab, select VSLangProj, VSLangProj2, and VSLangProj80, and then click OK.
Create a folder on your computer:
<Installation Root>\UserFiles\MyProjects\MyTestProject
In this example, the <Installation Root> is "C:".
Add the following using statements to the top of the Connect.cs file.
using VSLangProj; using VSLangProj2; using VSLangProj80; using VSLangProj90;
using VSLangProj100;Add the following method call to the OnConnection method.
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; VBVSProj2Manip(_applicationObject); }
Add the CSVSProj2Manip method declaration directly below the OnConnection method.
public void CSVSProj2Manip(DTE2 dte) { }
Add the following declarations to the top of the method.
Solution2 soln = (Solution2)_applicationObject.Solution; String vbTemplatePath; String vbPrjPath; Project proj; VSProject2 vsproj; Imports impCollection;
Use AddFromTemplate to create a Visual C# project.
- The syntax for obtaining the templates is EnvDTE80.Solution2.GetProjectTemplate("WindowsApplication.zip", "VisualBasic"), where the name "WindowsApplication.zip" is obtained from the WindowsApplication.zip file located in <Installation Root>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\VisualBasic\Windows\1033 folder. For all Visual Studio project types these files can be found in the <Installation Root>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Language folder. "VisualBasic" specifies that this project is a Visual Basic project.
// Make sure you create the folders that // make up the file path // on your computer. You can replace // this with your own file path. vbPrjPath = "C:\\UserFiles\\MyProjects\\MyTestProject"; // Get the project template path for a C# windows // application. vbTemplatePath = soln.GetProjectTemplate ("WindowsApplication.zip", "VisualBasic"); // Create a new Windows application by using the // template obtained above. soln.AddFromTemplate(vbTemplatePath, vbPrjPath, "Test2VBProj", false);
Add the following code to demonstrate the use of Imports after it is obtained through the Imports property.
proj = soln.Projects.Item(1); // Get a reference to the VSProject2 object. vsproj = (VSProject2)proj.Object; // Add a reference to System.Security.dll. MessageBox.Show("Adding a reference to System.Security.dll"); // Remove the <version number> in the following path // and replace it with one of the version // number folders that appear // in <installation root>\WINDOWS\Microsoft.NET\Framework // folder vsproj.References.Add ("C:\\WINDOWS\\Microsoft.NET\\Framework\\ <version number>\\System.Security.dll"); impCollection = vsproj.Imports; MessageBox.Show("The number of imports in this project is: " + impCollection.Count.ToString() + "\n"); MessageBox.Show ("Adding System.Security to the Imports collection."); impCollection.Add("System.Security"); MessageBox.Show("The number of imports in this project is now: " + impCollection.Count.ToString() + "\n"); String temp = null; for (int i = 1; i <= impCollection.Count; i++) { temp = temp + impCollection.Item(i).ToString() + "\n"; } MessageBox.Show("The Imports in this project are:" + "\n" + temp);
The VBVSProj2Manip method uses the VSProject2 object to:
Add a reference to System.Security.dll by using References.
The methods on Imports are used to:
Add the System.Security to the Imports collection by using Add.
Display the number of items in the Imports collection by using the Count property.
Display the name of the items in the Imports collection by using the Item method.
The example section lists the complete code including a try-catch block for the entire method.
To build the add-in, click Build Solution on the Build Menu.
Open a Visual Basic project in the Visual Studio integrated development environment (IDE).
On the Tools menu, click Add-in Manager, and select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.
Example
The following example is a basic Visual Studio add-in that demonstrates how to use the Imports property by using Visual Studio automation.
using System;
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
using VSLangProj90;
using VSLangProj100;
public void OnConnection(object application,
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
VBVSProj2Manip(_applicationObject);
}
public void VBVSProj2Manip(DTE2 dte)
{
try
{
Solution2 soln = (Solution2)_applicationObject.Solution;
String vbTemplatePath;
String vbPrjPath;
Project proj;
VSProject2 vsproj;
Imports impCollection;
// Make sure you create the folders that make up the file path
// on your computer. You can replace this with
// your own file path.
vbPrjPath = "C:\\UserFiles\\MyProjects\\MyTestProject";
// Get the project template path for a Visual Basic windows
// application.
vbTemplatePath = soln.GetProjectTemplate
("WindowsApplication.zip", "VisualBasic");
// Create a new Windows application by using the
// template obtained above.
soln.AddFromTemplate(vbTemplatePath, vbPrjPath,
"Test2VBProj", false);
proj = soln.Projects.Item(1);
// Cast to the VSProject2 object.
vsproj = (VSProject2)proj.Object;
// Add a reference to System.Security.dll.
MessageBox.Show("Adding a reference to System.Security.dll");
// Remove the <version number> in the following path
// and replace it with one of the version
// number folders that appear
// in <installation root>\WINDOWS\Microsoft.NET\Framework
// folder
vsproj.References.Add
("C:\\WINDOWS\\Microsoft.NET\\Framework\\
<version number>\\System.Security.dll");
vsproj.Refresh();
impCollection = vsproj.Imports;
MessageBox.Show("The number of imports in this project is: "
+ impCollection.Count.ToString() + "\n");
MessageBox.Show("Adding System.Security to the
Imports collection.");
impCollection.Add("System.Security");
MessageBox.Show("The number of imports in this project is now:
" + impCollection.Count.ToString() + "\n");
String temp = null;
for (int i = 1; i <= impCollection.Count; i++)
{
temp = temp + impCollection.Item(i).ToString() + "\n";
}
MessageBox.Show("The Imports in this project are:" + "\n"
+ temp);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports VSLangProj
Imports VSLangProj2
Imports VSLangProj80
Imports VSLangProj90
Imports VSLangProj100
Public Sub OnConnection(ByVal application As Object, _
ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
VBVSProj2Manip(_applicationObject)
End Sub
Sub VBVSProj2Manip(ByVal dte As DTE2)
Try
Dim soln As Solution2 = CType(_applicationObject.Solution, _
Solution2)
Dim vbTemplatePath As String
Dim vbPrjPath As String
Dim proj As Project
Dim vsproj As VSProject2
Dim impCollection As [Imports]
' Create this or your own file path on your computer.
' The file path needs to exist before you run this add-in.
vbPrjPath = "C:\UserFiles\MyProjects\MyTestProject"
' Get the project template path for a Visual Basic
' Windows application.
vbTemplatePath = soln.GetProjectTemplate _
("WindowsApplication.zip", "VisualBasic")
' Create a new Windows Application by using the
' template obtained above.
soln.AddFromTemplate(vbTemplatePath, vbPrjPath, _
"Test2JSProj", False)
proj = soln.Projects.Item(1)
' Cast the project to a VSProject2.
vsproj = CType(proj.Object, VSProject2)
' Add a reference to System.Security.dll.
MsgBox("Adding a reference to System.Security.dll")
' Remove the <version number> in the following path
' and replace it with one of the version
' number folders that appear
' in <installation root>\WINDOWS\Microsoft.NET\Framework
' folder
vsproj.References.Add _
("C:\WINDOWS\Microsoft.NET\Framework\ _
<version number>\System.Security.dll")
impCollection = vsproj.Imports
MsgBox("The number of imports in this project is: " & vbCr _
& impCollection.Count.ToString())
MsgBox("Adding System.Security to the Imports collection.")
impCollection.Add("System.Security")
MsgBox("The number of imports in this project is now: " _
& vbCr & impCollection.Count.ToString())
Dim temp As String = ""
For i As Integer = 1 To impCollection.Count
temp = temp & impCollection.Item(i).ToString() & vbCr
Next i
MsgBox("The Imports in this project are:" & vbCr & temp)
Catch ex As System.Exception
MsgBox(ex.ToString)
End Try
End Sub
Compiling the Code
To compile this code, create a new Visual Studio Add-in project and replace the code of the OnConnection method with the code in the example. For information about how to run an add-in, see How to: Control Add-Ins By Using the Add-In Manager.
See Also
Concepts
Introduction to the VSProject2 Object