Export (0) Print
Expand All

Globals.VariablePersists Property

The VariablePersists property applies to several types of Globals objects. For the DTE.Globals object, it gets or sets whether the variable is retained by the environment and is available between sessions of the environment. For the Solution.Globals object, it gets or sets whether the variable is retained by the environment and is available between sessions of the environment and between loading and unloading of a solution. For the Project.Globals object, it gets or sets whether the variable is retained by the environment in the project file.

Namespace:  EnvDTE
Assembly:  EnvDTE (in EnvDTE.dll)

bool this[
	string VariableName
] { get; set; }

Parameters

VariableName
Type: System.String

Required. Represents the name of the variable to retain.

Property Value

Type: System.Boolean
A Boolean value indicating whether or not a variable exists. VariablePersists returns true if a variable exists, otherwise returns false.

Although Global variables always persist within a session of Visual Studio, VariablePersists allows these variables to persist between sessions.

NoteNote

To save variables with a particular solution, use DTE.Solution.Globals.

If the variable does not exist, VariablePersists returns false.

For the Solution object (Solution.Globals), data is saved whenever the solution is saved. Modifying the Globals object causes the solution file to become marked as edited (or "dirty"). For the DTE object (DTE.Globals), data is saved either when the Visual Studio environment is shut down or when a solution is saved. In both cases, the data is stored either in the solution (.sln) file or in the structured storage file in the User Profiles directory.

When the environment is shut down or a Save All occurs, all global values are saved. If VariablePersists is associated with the DTE object, the value is saved in the user options directory of the Visual Studio environment.

If the global variable is associated with the Solution object, then the value is saved in the solution (.sln) file. The values are saved any time the environment writes the .sln file.

Any saved variables overwrite previously saved values. To remove a variable from the saved file, set VariablePersists to false. The environment will remove its value during the next Save operation.

NoteNote

VariableValue names cannot contain spaces. If one does, you get the error, Value does not fall within expected range.

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

Show:
© 2014 Microsoft