在 Visual Studio 中使用 EnvDTE 时,DTE.Globals 持久变量实际存储在哪里?

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

来自 Globals.VariablePersists Property 文档:

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.

我已经成功地在 Solution.Globals 对象上使用了 VariablePersists 属性 并按照描述将其保存到解决方案 (.sln) 文件中。我对 DTE.Globals 做了同样的事情,但我找不到实际的存储位置。

我找到的最接近的是 %LOCALAPPDATA%\Microsoft\VisualStudio.0_50f510cf\Settings 中的 CurrentSettings.vssettings 文件,但我在文件中搜索持久变量(名称或值)时没有结果。

我还查看了以下目录(及其相关子目录),希望找到文档中提到的“用户选项目录”或“用户配置文件目录”:

这个结构化存储文件是什么,在哪里可以找到(我用的是Visual Studio 2019)?

我将尽我目前对 Visual Studio 2019 SDK 的了解来回答我自己的问题。如果有人有更好的解释或我可能遗漏的内部知识,欢迎分享。

Visual Studio 2019 SDK 文档详细介绍了 Globals.VariablePersists[String] Property 的工作原理:

“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.”

知道这一点,我做了一个简单的项目来测试我是否可以在 DTE.Globals 对象上持久化一个变量并在 Visual Studio 会话之间读取它的值。这是它的要点:

   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;

运行 它在同一个 Visual Studio 实例中多次按预期工作:全局变量在第一个 运行 上分配,其当前值在后续 [=42] 中仍然可用=]秒。保存解决方案后,重新启动 Visual Studio 实例,加载并再次 运行ning 项目,我注意到该变量不再存在:它不会在 Visual Studio 会话之间保留该变量。这不是我期望的行为。

在尝试查找文档中提到的结构化存储文件时,我注意到 %LOCALAPPDATA%\Microsoft\VisualStudio.0_50f510cf\ApplicationPrivateSettings.xml 文件在文件开头包含一个空的 <globals /> 标记。我认为这可能是指定持久 DTE 全局变量列表的预期位置。但这是一个猜测,正如我所说,它始终是空的,即使在 运行 测试项目并保存解决方案之后也是如此。

长话短说,我希望添加到 DTE.Globals 属性 的变量可以保存在位于当前用户配置文件中某处的存储文件中,并且根据我目前的理解,事实并非如此。似乎添加到 DTE.Globals 对象的变量实际上只在 Visual Studio 会话期间保存在临时缓存中。如果这是预期的行为,文档可能需要对其进行澄清。

出于我的目的,我将坚持将全局变量持久化到解决方案文件中,因为这种方法易于验证并且有效。