HOW TO:管理 Visual Basic 專案的 Imports 屬性
更新:2007 年 11 月
大多數的 VSProject2 方法和屬性都可適用於 Visual C#、Visual Basic 和 Visual J# 專案。如需詳細資訊,請參閱 HOW TO:管理 Visual Basic 和 C# 專案和 VSProject2 物件。VSProject2 物件的 Imports 屬性是 Visual Basic 專案特有的屬性,它可以用加入及列舉 Imports 集合的方法來存取 Imports 物件。
在下列步驟中,會說明如何使用 Visual Studio 增益集 (Add-In) 以程式設計方式控制 Visual Basic 專案中的 Imports 屬性。
注意事項: |
---|
根據目前使用的設定與版本,您所看到的對話方塊與功能表命令可能會與 [說明] 中所描述的不同。使用 [一般開發設定] 開發了這些程序。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定。 |
若要使用 VSProject2 物件控制 Visual Basic 專案
使用 Visual C# 建立 Visual Studio 增益集專案。
在 [專案] 功能表上按一下 [加入參考],再按一下 [.NET] 索引標籤,選取 [VSLangProj]、[VSLangProj2] 和 [VSLangProj80],然後再按 [確定]。
在電腦上建立一個資料夾:
<Installation Root>\UserFiles\MyProjects\MyTestProject
在此範例中,<Installation Root> 為 "C:"。
將下列 using 陳述式加入至 Connect.cs 檔的頂端。
using VSLangProj; using VSLangProj2; using VSLangProj80;
將下列方法呼叫加入至 OnConnection 方法。
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; VBVSProj2Manip(_applicationObject); }
緊接在 OnConnection 方法的下面加入 CSVSProj2Manip 方法宣告。
public void CSVSProj2Manip(DTE2 dte) { }
將下列宣告加入至方法的最上方。
Solution2 soln = (Solution2)_applicationObject.Solution; String vbTemplatePath; String vbPrjPath; Project proj; VSProject2 vsproj; Imports impCollection;
使用 AddFromTemplate 建立 Visual C# 專案。
- 取得範本的語法為 EnvDTE80.Solution2.GetProjectTemplate("WindowsApplication.zip", "VisualBasic"),其中 "WindowsApplication.zip" 這個名稱是根據位於 <Installation Root>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\VisualBasic\Windows\1033 資料夾中的 WindowsApplication.zip 檔案而來。對於所有 Visual Studio 專案類型來說,這些檔案都可以在 <Installation Root>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Language 資料夾中找到。"VisualBasic" 則是指定這個專案為 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);
加入下列程式碼,示範在透過 Imports 屬性取得 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);
VBVSProj2Manip 方法使用 VSProject2 物件的目的:
使用 References 加入 System.Security.dll 的參考。
使用 Imports 上之方法的目的:
範例部分會列出完整的程式碼,包括整個方法的 try-catch 區塊。
若要建置增益集,請按一下 [建置] 功能表上的 [建置方案]。
在 Visual Studio 整合式開發環境 (IDE) 中開啟 Visual Basic 專案。
按一下 [工具] 功能表上的 [增益集管理員],然後從 [增益集管理員] 對話方塊中選取增益集。按一下 [確定],執行您的增益集。
範例
下列是基本 Visual Studio 增益集的範例,在此範例中會示範如何藉由 Visual Studio Automation 使用 Imports 屬性。
using System;
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
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
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
編譯程式碼
若要編譯這個程式碼,請建立新的 Visual Studio 增益集專案,並以範例中的程式碼取代 OnConnection 方法的程式碼。如需如何執行增益集的詳細資訊,請參閱 HOW TO:以增益集管理員控制增益集。