VSWebSite 介面
提供網站專案的屬性和方法。
命名空間: VsWebSite
組件: VsWebSite.Interop (在 VsWebSite.Interop.dll 中)
語法
'宣告
<GuidAttribute("70758CA4-91F8-46AD-80A7-73BC21BAE68B")> _
Public Interface VSWebSite
[GuidAttribute("70758CA4-91F8-46AD-80A7-73BC21BAE68B")]
public interface VSWebSite
[GuidAttribute(L"70758CA4-91F8-46AD-80A7-73BC21BAE68B")]
public interface class VSWebSite
[<GuidAttribute("70758CA4-91F8-46AD-80A7-73BC21BAE68B")>]
type VSWebSite = interface end
public interface VSWebSite
VSWebSite 類型會公開下列成員。
屬性
名稱 | 描述 | |
---|---|---|
CodeFolders | 取得在網站中設定為程式碼資料夾的資料夾集合。 | |
DTE | 取得包含此網站專案之 DTE2 物件的參考。 | |
Project | 取得這個網站的參考,當做 Project 物件。 | |
References | 取得 AssemblyReferences 物件,其中包含目前網站之組件和專案的參考。 | |
TemplatePath | 取得包含網站項目範本之資料夾的完整路徑和名稱。 | |
URL | 取得用來開啟網站的 URL。 | |
UserTemplatePath | 取得新專案項目之使用者範本資料夾的路徑。 | |
VSWebSiteEvents | 取得網站的 VSWebSiteEvents 物件,這個物件可用來加入事件處理常式。 | |
WebReferences | 取得 WebReferences 物件,其中包含網站所耗用之 Web 服務的參考。 | |
WebServices | 取得 WebServices 物件,其中包含這個網站所公開 (Expose) 之 Web 服務的集合。 |
回頁首
方法
名稱 | 描述 | |
---|---|---|
AddFromTemplate | 在網站專案中建立新的 ProjectItem。 | |
EnsureServerRunning | 必要時,啟動 ASP.NET 程式開發伺服器 (Development Server),然後傳回網站的 URL。 | |
GetUniqueFilename | 使用指定的根目錄名稱和副檔名,傳回在指定之資料夾內唯一的檔案名稱。 | |
PreCompileWeb | 編譯網站並將編譯的輸出寫入指定的資料夾。 | |
Refresh | 針對在 Visual Studio 以外對網站所做的變更,重新整理帳戶的顯示。 | |
WaitUntilReady | 封鎖所有方法呼叫,直到背景處理序 (Process) 執行完成為止。 |
回頁首
備註
您可以使用 VSWebSite 介面,從巨集或 Visual Studio 的增益集 (Add-In) 管理網站專案。
除了這個類別 (Class) 中的屬性和方法之外,使用 WebSiteProperties 類別時,還有更多網站專案的屬性可用。
注意事項 |
---|
從 Visual Studio 2005 開始,這個類別所提供的功能均可在 Visual Studio 的版本中使用。無法在 Visual Web Developer Express 版中使用。 |
範例
下列範例將說明如何使用增益集與 Visual Studio 網站專案進行互動。 當組件 (Assembly) 的參考或 Web 服務的 Web 參考加入至專案時,此增益集會使用事件處理常式來寫入事件記錄檔。 此外,關閉方案時,它會將每個網站專案的摘要寫入文字檔。
若要執行這個範例,請使用 如何:建立增益集 來建立增益集專案,然後將產生之 Connect.cs 檔中的所有程式碼取代成範例程式碼。 此外,您還需要建立 VsWebSite.Interop 組件的參考。
[C#]
using System;
using System.IO;
using System.Collections;
using System.Diagnostics;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using VsWebSite;
// The object for implementing an Add-in.
public class Connect : IDTExtensibility2
{
private DTE2 _applicationObject;
private AddIn _addInInstance;
// Implements the constructor for the Add-in object.
// Created by the Add-In Wizard
public Connect()
{
}
// Event method created by the Add-In Wizard.
// Occurs when the Add-In connects with the application.
public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
// Attach Solution event handlers
_applicationObject.DTE.Events.SolutionEvents.Opened
+= new _dispSolutionEvents_OpenedEventHandler
(SolutionEvents_Opened);
_applicationObject.DTE.Events.SolutionEvents.QueryCloseSolution
+= new _dispSolutionEvents_QueryCloseSolutionEventHandler
(SolutionEvents_QueryCloseSolution);
}
// Custom event method that occurs before a solution is closed.
private void SolutionEvents_QueryCloseSolution(ref bool fCancel)
{
foreach (Project proj in _applicationObject.Solution.Projects)
{
// Make sure background compilation is finished
((VSWebSite)proj.Object).WaitUntilReady();
System.Text.StringBuilder strBldr =
new System.Text.StringBuilder();
strBldr.AppendLine("Summary for Web Site: "
+ ((VSWebSite)proj.Object).URL);
strBldr.AppendLine("Solution: "
+ _applicationObject.Solution.FullName);
strBldr.AppendLine("Web References:");
foreach (WebReference wref in
((VSWebSite)proj.Object).WebReferences)
strBldr.AppendLine(" " + wref.Namespace);
strBldr.AppendLine("Assembly References:");
foreach (AssemblyReference aref in
((VSWebSite)proj.Object).References)
strBldr.AppendLine(" " + aref.Name);
// list the files?
((VSWebSite)proj.Object).VSWebSiteEvents.WebReferencesEvents.WebReferenceAdded
-= WebRefEvents_WebRefAdded;
string strBody = strBldr.ToString();
// Save the summary as a text file in the Web site.
string fName = _applicationObject.FullName;
fName = fName.Substring(0, fName.Length - 4);
fName += "." + ((VSWebSite)proj.Object).GetUniqueFilename
("/", "ProjectSummary", ".txt");
if (File.Exists(fName))
File.Delete(fName);
StreamWriter sw = File.CreateText(fName);
sw.Write(strBody);
sw.Close();
}
}
// Custom event method that occurs when a solution is opened.
private void SolutionEvents_Opened()
{
// When solution is opened, attach event handlers for projects
foreach (Project proj in _applicationObject.Solution.Projects)
{ // Only attach event handlers if it is a Web site
if (proj.Object is VSWebSite)
{
((VSWebSite)proj.Object).VSWebSiteEvents.WebReferencesEvents.WebReferenceAdded +=
new _dispWebReferencesEvents_WebReferenceAddedEventHandler
(WebRefEvents_WebRefAdded);
((VSWebSite)proj.Object).VSWebSiteEvents.AssemblyReferencesEvents.AssemblyReferenceAdded +=
new _dispAssemblyReferencesEvents_AssemblyReferenceAddedEventHandler
(AssemblyRefsEvents_AssemblyRefAdded);
}
}
}
// Custom event method that occurs when a Reference is added.
private void AssemblyRefsEvents_AssemblyRefAdded(AssemblyReference AssemblyRef)
{
EventLog appLog = new EventLog();
appLog.Source = "VSWSTest." + AssemblyRef.ContainingProject.Name;
appLog.WriteEntry("AssemblyReference added: " + AssemblyRef.Name);
}
// Custom event method that occurs when a Web Reference is added.
private void WebRefEvents_WebRefAdded(WebReference webRef)
{
EventLog appLog = new EventLog();
appLog.Source = "VSWSTest." + webRef.ContainingProject.Name;
appLog.WriteEntry("WebReference added: " + webRef.Namespace);
}
#region Required but unused event handlers
/// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
/// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
{
}
/// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref Array custom)
{
}
/// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref Array custom)
{
}
/// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref Array custom)
{
}
#endregion
}