visual-studioenvdte

When using EnvDTE in Visual Studio, where are the DTE.Globals persisted variables actually stored?


From the Globals.VariablePersists Property documentation:

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.

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.

... In both cases, the data is stored either in the solution (.sln) file or in the structured storage file in the User Profiles directory.

I have successfully used the VariablePersists property on a Solution.Globals object and it saved to the solution (.sln) file as described. I did the same with the DTE.Globals, but I couldn't find the actual storage location.

The closest I've found is a CurrentSettings.vssettings file in %LOCALAPPDATA%\Microsoft\VisualStudio\16.0_50f510cf\Settings, but I had no result searching for the persisted variables (name or value) within the file.

I've also looked in the following directories (and their relative sub-directories) hoping to find this "user options directory" or "User Profiles directory" as mentioned in the documentation:

What is this structured storage file and where can it be found (I'm using Visual Studio 2019)?


Solution

  • I’m answering my own question to the best of my current knowledge of Visual Studio 2019 SDK. If anyone has a better explanation or inside knowledge I may have missed, you’re welcome to share it.

    The Visual Studio 2019 SDK documentation has details on how the Globals.VariablePersists[String] Property works:

    “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 DTE object (DTE.Globals), data is saved either when the Visual Studio environment is shut down or when a solution is saved.” ...

    “If VariablePersists[] is associated with the DTE object, the value is saved in the user options directory of the Visual Studio environment.”

    Knowing this, I made a simple project to test if I could persist a variable on the DTE.Globals object and read its value between Visual Studio sessions. Here’s the gist of it:

       string name = "MY_TEST_VARIABLE";
    
       if (_dte.Globals.VariableExists[name])
       {
           Console.WriteLine("The current value for variable {0}: {1}", name, _dte.Globals[name]);
       }                    
    
       _dte.Globals[name] = "MY_VALUE";
       _dte.Globals.VariablePersists[name] = true;
    

    Running it multiple times within the same Visual Studio instance worked as intended: the global variable is assigned on the first run and its current value remains available in subsequent runs. After saving the solution, restarting the Visual Studio instance, loading and running the project again, I noticed that the variable did not exists anymore: it did not persist the variable between Visual Studio sessions. This was not the behavior I was expecting.

    While trying to locate the structured storage file mentioned in the documentation, I noticed that the %LOCALAPPDATA%\Microsoft\VisualStudio\16.0_50f510cf\ApplicationPrivateSettings.xml file contains an empty <globals /> tag at the beginning of the file. I would think that this could be the expected location to specify a list of persisted DTE’s Globals variables. But its a guess, as I said it was always empty, even after running the test project and saving the solution.

    Long story short, I was expecting that variables that are added to the DTE.Globals property could be persisted in a storage file located somewhere within the current user profile and, from my current understanding, it is not the case. It seems that variables added to the DTE.Globals object are actually only saved in a temporary cache for the duration of the Visual Studio session. If this is the intended behavior, the documentation might need to clarify it.

    For my purpose, I’ll stick with persisting globals variables to the solution file as this method is easy to verify and it works.