Globals.VariablePersists Property (String)


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; }


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.


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.


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

void GlobalsExample(_DTE applicationObject)
    Globals globals;
    globals = applicationObject.Solution.Globals;
        // The counter has already been set, so increment it.
        System.Int32 int32;
        int32 = System.Int32.Parse((string)
        globals["GlobalsExample"] = int32.ToString();
        // Counter has never been set, so create and initialize it.
        globals["GlobalsExample"] = 1.ToString();
        globals.set_VariablePersists("GlobalsExample", true);
    System.Windows.Forms.MessageBox.Show("This variable has been called: 
    " + globals.VariableValue["GlobalsExample"] + " times.");
Return to top