Dela via


Persisting Information in Projects and Solutions

When you close a project, user data such as variable values are discarded, even if you save the solution or project. The Visual Studio automation model, however, offers a way to store, or persist, such user data between sessions of the integrated development environment (IDE). This is accomplished by using the Globals object through the Globals and Globals properties. Globals persists solution variables and Globals persists project variables. Each property returns a Globals object whose members allow you to store, retrieve, enumerate, and optionally persist the data. When you do this, the next time you open the solution or project, the values are restored.

This is useful, for example, to allow a command to offer a persistent default value, or to allow it to change its behavior after it has been invoked a specific number of times. Add-ins can also use this feature to persist data to and retrieve data from Solution (.sln) files.

Global Object Behavior Details

If the Globals object is associated with the IDE, then the value persists in one of two locations. For Windows NT 4.0, Windows 2000 Professional, and Windows Server 2003, the values are stored in C:\winnt\Profiles\<username>\Application Data\Microsoft\Visual Studio\extglobal.dat. For Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, if the machine is setup for user login, the values are stored in C:\Windows\Profiles\<username>\Application Data\Microsoft\Visual Studio\extglobal.dat. Otherwise, there is no <username> element. Each time the IDE is closed or a Save All operation occurs, the IDE persists the global values.

If the Globals object is associated with the Solution2 object, then the value persists in the .sln file. These values are persisted when the solution file is saved.

If the Globals object is associated with a Project object, then the value persists in the project file (.dsp, .vbp, and so on). The values are persisted anytime a project is saved.

Values to be stored must be as a string that can be persisted — that is, not a SAFEARRAY, object, or structured storage. If the variable cannot be converted to a string, an English string value is persisted that explains why the variable was not persisted.

Whenever variables are persisted, a new record of the variables and their values is saved.

Persisting Global Values

The following macro example shows how to use the Globals object and its members to retain the value of a variable after a solution is closed and how to access the value when the solution is reopened. It counts and outputs the number of times the add-in has been loaded.

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.");
}

See Also

Tasks

How to: Add and Handle Commands

How to: Create an Add-In

Walkthrough: Creating a Wizard

Concepts

Automation Object Model Chart

Other Resources

Creating and Controlling Environment Windows

Creating Add-ins and Wizards

Automation and Extensibility Reference