Share via


Persisting Information in Projects and Solutions

When you close a project, user data such as variable values are usually discarded, even if you save the solution or project. The Visual Studio .NET automation model, however, offers a way to store, or persist, such user data between sessions of the integrated development environment (IDE). This is accomplished using the Globals object through the Solution.Globals and DTE.Globals properties. Solution.Globals persists solution variables and DTE.Globals persists project variables. Both properties returns a Globals object that allows you to store, retrieve, enumerate, and optionally persist the data. When you do this, the next time you open the solution and/or projects, 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 certain number of times. Add-ins can also use this feature to persist and retrieve data to and 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 and Windows 2000 Professional, the values are stored in "C:\winnt\Profiles\<username>\Application Data\Microsoft\Visual Studio\extglobal.dat".   For Windows 9x/ME/XP, 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 Solution object, then the value persists in the .sln file. These values are persisted anytime the solution file is saved.

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

Values to be stored must be persistable as a string — that is, not a SAFE-ARRAY, 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.

Globals Example

The following example shows how to use the Globals object to retain the value of a variable after a solution is closed and how to access the value later, when the solution is reopened.

Private objSoln As Solution

Public Property prop(ByVal somestuff As String)
   Get
      objSoln.Globals.VariableValue("SampleProp") = somestuff
      objSoln.Globals.VariablePersists("SampleProp") = True
   End Get
   Set(ByVal SampleProp)
      If Not objSoln.Globals.VariableExists("SampleProp") Then
         objSoln.Globals.VariableValue("SampleProp") = ""
         objSoln.Globals.VariablePersists("SampleProp") = True
      End If
      SampleProp = objSoln.Globals.VariableValue("SampleProp")
   End Set
End Property

See Also

Introduction to Solutions, Projects, and Items | Adding and Handling Commands | Creating and Controlling Environment Windows | Creating Add-Ins and Wizards | Creating an Add-In | Creating a Wizard | Automation and Extensibility Reference | Automation Object Model Chart