Condividi tramite


Procedura: modificare la proprietà Imports dei progetti Visual Basic

La maggior parte dei metodi e delle proprietà VSProject2 sono applicabili ai progetti Visual C# e Visual Basic. Per ulteriori informazioni, vedere Procedura: modificare progetti Visual Basic e C# tramite l'oggetto VSProject2. La proprietà Imports dell'oggetto VSProject2 è specifica dei progetti Visual Basic. Consente di accedere all'oggetto Imports con metodi per l'aggiunta e l'enumerazione dell'insieme Imports.

Nella procedura riportata di seguito viene illustrato come controllare a livello di codice la proprietà Imports in un progetto Visual Basic mediante un componente aggiuntivo di Visual Studio.

Nota

È possibile che le finestre di dialogo e i comandi di menu visualizzati siano diversi da quelli descritti nella Guida a seconda delle impostazioni attive o dell'edizione del programma. Queste procedure sono state sviluppate con le Impostazioni generali per lo sviluppo attive. Per modificare le impostazioni, scegliere Importa/Esporta impostazioni dal menu Strumenti. Per ulteriori informazioni, vedere Gestione delle impostazioni.

Per utilizzare l'oggetto VSProject2 per controllare i progetti Visual Basic

  1. Creare un progetto di componente aggiuntivo Visual Studio utilizzando Visual C#.

  2. Scegliere Aggiungi riferimento dal menu Progetto, selezionare la scheda .NET, quindi VSLangProj, VSLangProj2 SLangProj80, quindi scegliere OK.

  3. Creare una cartella sul computer:

    • <Directory radice di installazione>\UserFiles\MyProjects\MyTestProject.

      In questo esempio, la <Directory radice di installazione> è "C:".

  4. Aggiungere le istruzioni using riportate di seguito all'inizio del file Connect.cs.

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    using VSLangProj90;
    
  5. using VSLangProj100;Aggiungere la chiamata seguente al metodo OnConnection:

    public void OnConnection(object application, 
    ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        VBVSProj2Manip(_applicationObject);
    }
    
  6. Aggiungere la dichiarazione del metodo CSVSProj2Manip direttamente sotto il metodo OnConnection.

    public void CSVSProj2Manip(DTE2 dte)
    {
    }
    
  7. Aggiungere le seguenti dichiarazioni all'inizio del metodo.

    Solution2 soln = (Solution2)_applicationObject.Solution;
    String vbTemplatePath;
    String vbPrjPath;
    Project proj;
    VSProject2 vsproj;
    Imports impCollection;
    
  8. Utilizzare il metodo AddFromTemplate per creare un progetto Visual C#.

    • La sintassi per ottenere i modelli è EnvDTE80.Solution2.GetProjectTemplate("WindowsApplication.zip", "VisualBasic"), dove il nome "WindowsApplication.zip" si ottiene dal file WindowsApplication.zip contenuto nella cartella <Directory radice di installazione>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\VisualBasic\Windows\1033. Per tutti i tipi di progetti di Visual Studio è possibile trovare questi file nella cartella <Radice di installazione>\Programmi\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Linguaggio. "VisualBasic" specifica che il progetto in questione è un progetto Visual Basic.
    // 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);
    
  9. Aggiungere il codice riportato di seguito per illustrare l'utilizzo dell'oggetto Imports dopo che è stato ottenuto attraverso la proprietà Imports.

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

    Il metodo VBVSProj2Manip utilizza l'oggetto VSProject2 per:

    • Aggiungere un riferimento a System.Security.dll mediante la proprietà References.

    • Ottenere un handle per l'oggetto Imports mediante la proprietà Imports.

    I metodi dell'oggetto Imports vengono utilizzati per:

    • Aggiungere System.Security all'insieme Imports mediante il metodo Add.

    • Visualizzare il numero di elementi dell'insieme Imports mediante la proprietà Count.

    • Visualizzare il nome degli elementi dell'insieme Imports mediante il metodo Item.

    Nella sezione relativa agli esempi è elencato il codice completo, incluso un blocco try-catch per l'intero metodo.

  10. Scegliere Compila soluzione dal menu Compila per compilare il componente aggiuntivo.

  11. Aprire un progetto Visual Basic nell'ambiente di sviluppo integrato (IDE) di Visual Studio.

  12. Scegliere Gestione componenti aggiuntivi dal menu Strumenti, quindi selezionare il componente aggiuntivo dalla finestra di dialogo Gestione componenti aggiuntivi. Scegliere OK per eseguire il componente aggiuntivo.

Esempio

Nell'esempio seguente viene utilizzato un componente aggiuntivo di base di Visual Studio per illustrare l’utilizzo della proprietà Imports mediante l'automazione Visual Studio.

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

Compilazione del codice

Per compilare il codice, creare un nuovo progetto di componente aggiuntivo Visual Studio e sostituire il codice del metodo OnConnection con il codice dell'esempio. Per informazioni su come eseguire un componente aggiuntivo, vedere Procedura: controllare i componenti aggiuntivi tramite Gestione componenti aggiuntivi.

Vedere anche

Concetti

Introduzione all'oggetto VSProject2

Altre risorse

Estensione di progetti Visual Basic e Visual C#