Persisting Solution and Project Information Across IDE Sessions


At times, your add-in or macro might need to save some data that should be carried along with the solution or project file. The object model supports saving information into these files with the EnvDTE.Globals object. You can find this object by calling the Globals property of both of these objects:

 Sub SolutionGlobals()     Dim globals As EnvDTE.Globals     globals = DTE.Solution.Globals End Sub Sub ProjectGlobals()     Dim globals As EnvDTE.Globals     globals = DTE.Solution.Projects.Item(1).Globals End Sub 

The Globals object of the Solution and Project objects works in much the same way as the Globals object found on the DTE object, with a few minor differences. First, if a macro or an add-in stores data into the solution or project file, even if the VariablePersists flag is set for that variable, the data might not be written into the solution or project file. This is because making a change to a variable causes the project or solution file to be put into a modified state. If you close the solution or project file but do not choose to save the modified files, the data won't be written into that file. Second, unlike the EnvDTE.Globals object on the DTE object, which can store data in a wide variety of formats, data stored into a solution or project file can be stored only in string format. This is because project and solution files are text-based, so any data stored into these files must also be in a text format. This doesn't mean that nonstring data can't be stored into the solution or project Globals object. It just means that when the data is to be written into the solution or project files, an attempt will be made to convert the data into a string. If that fails, the data won't be stored. Also, because the data is converted into a string when it is stored into the solution or project files, when the Globals object is restored from the solution or project file, this data will also be in a string format. It is up to the macro or add-in code to properly determine which format the data is in.

A good use of the Globals object is to keep track of the number of times you build a project. I like to count the number of times I build a project. Not that this number has any significance, but it is just an interesting fact. The following macro sample is an implementation of the OnBuildDone event. As each OnBuildDone event is fired, the sample checks for the existence of the BuildCounter variable within the solution Globals object. If this value exists, it is incremented and stored back into the Globals object. If this value doesn't exist, the value 1 is stored. The code for the OnBuildDone event is shown here:

     Private Sub BuildEvents_OnBuildDone(ByVal Scope As _         EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) _         Handles BuildEvents.OnBuildDone         'Increment the build counter by storing a value in the         ' solution file through the Globals object:         Dim globals As Globals         Dim int32 As System.Int32         globals = DTE.Solution.Globals         If (globals.VariableExists("BuildCounter")) Then             'A counter has been set, increment it:             int32 = System.Int32.Parse _                (globals.VariableValue("BuildCounter").ToString())             int32 = int32 + 1             globals.VariableValue("BuildCounter") = int32.ToString()             globals.VariablePersists("BuildCounter") = True         Else             'The variable has never been set, seed the counter:             globals.VariableValue("BuildCounter") = 1.ToString()             globals.VariablePersists("BuildCounter") = True         End If     End Sub 




Working with Microsoft Visual Studio 2005
Working with Microsoft Visual Studio 2005
ISBN: 0735623155
EAN: 2147483647
Year: 2006
Pages: 100

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net