共用方式為


保存專案與方案中的資訊

即使儲存了方案或專案,在關閉專案時,仍然會捨棄像是變數值等的使用者資料。 不過,Visual Studio Automation 模型提供了一種方式,可以在整合式開發環境 (IDE) 的不同工作階段之間儲存 (或保存) 這類使用者資料。 這是利用 Globals 物件,透過 GlobalsGlobals 屬性所達成的。 Globals  會保存方案變數,而 Globals  則會保存專案變數。 每個屬性都會傳回 Globals 物件,其成員可讓您儲存、擷取、列舉,及選擇性保存資料。 執行此作業後,下次再開啟方案或專案時,就會將這些值還原。

如果要讓命令提供持續性 (Persistent) 預設值,或者要在叫用指定次數後允許命令變更其行為,這將會很有用。 增益集也可以使用此功能將資料保存至方案 (.sln) 檔,以及從方案檔擷取資料。

Global 物件行為詳細資料

如果 Globals 物件與 IDE 關聯,這些值會保存在以下其中一個位置。 在 Windows NT 4.0、Windows 2000 Professional 和 Windows Server 2003 中,這些值會儲存在 C:\winnt\Profiles\<username>\Application Data\Microsoft\Visual Studio\extglobal.dat。 而在 Windows 95、Windows 98、Windows 98 Second Edition、Windows Millennium Edition 中,如果電腦已設定讓使用者登入,則這些值會儲存在 C:\Windows\Profiles\<username>\Application Data\Microsoft\Visual Studio\extglobal.dat, 否則不會有 <username> 項目。 每次關閉 IDE 或發生全部儲存作業時,IDE 就會保存全域值。

如果 Globals 物件與 Solution2 物件關聯,這些值會保存在 .sln 檔案中。 儲存方案檔時,就會保存這些值。

如果 Globals 物件與 Project 物件關聯,則這些值會保存在專案檔 (.dsp、.vbp 等) 中。 只要儲存專案,就會保存值。

要儲存的值必須能當成字串保存,也就是說,並非 SAFEARRAY、物件或結構化儲存體。 如果無法將變數轉換為字串,將會保存一個英文字串值,說明為何沒有保存變數。

只要保存變數,就會儲存變數及其值的新記錄。

保存全域值

下列巨集範例示範如何使用 Globals 物件及其成員,在方案關閉後保存變數的值,以及如何在重新開啟方案時存取值。 它會計算並輸出增益集的載入次數。

Sub OnAddinLoaded(ByVal dte As DTE)
    ' Count the number of times an add-in is loaded
    ' and store the value in the solution.
    Dim globals As Globals
    globals = dte.Solution.Globals
    If globals.VariableExists("AddinLoadCounter") Then
        ' The counter has already been set, so increment it.
        Dim int32 As System.Int32
        int32 = System.Int32.Parse(CStr(globals("AddinLoadCounter")))
        int32 += 1
        globals("AddinLoadCounter") = int32.ToString()
    Else
        ' Counter has never been set, so create and initialize it.
        globals("AddinLoadCounter") = 1.ToString()
        globals.VariablePersists("AddinLoadCounter") = True
    End If
    MsgBox("This add-in has been loaded: " & _
    globals.VariableValue("AddinLoadCounter") & " times.")
End Sub
void OnAddinLoaded(_DTE applicationObject)
{
    // Count the number of times an add-in is loaded
    // and store the value in the solution.
    Globals globals;
    globals = applicationObject.Solution.Globals;
    if(globals.get_VariableExists("AddinLoadCounter"))
    {
        // The counter has already been set, so increment it.
        System.Int32 int32;
        int32 = System.Int32.Parse((string)
        globals["AddinLoadCounter"]);
        int32++;
        globals["AddinLoadCounter"] = int32.ToString();
    }
    else
    {
        // Counter has never been set, so create and initialize it.
        globals["AddinLoadCounter"] = 1.ToString();
        globals.set_VariablePersists("AddinLoadCounter", true);
    }
    System.Windows.Forms.MessageBox.Show("This add-in has been loaded: 
    " + globals.VariableValue["AddinLoadCounter"] + " times.");
}

請參閱

工作

HOW TO:新增和處理命令

HOW TO:建立增益集

逐步解說:建立精靈

概念

Automation 物件模型圖表

其他資源

建立和控制環境視窗

建立增益集和精靈

Automation 與擴充性參考