演练:使用设置单元

尽管 Visual Studio 将在系统注册表通常保留,通过使用设置存储区,您可以组离散从注册表中存储。 使用集而不是注册表中存储简化部署并提高安全性。

有两组存储区:

  • 配置设置,是只读 Visual Studio 和 VSPackage 设置。 Visual Studio 将设置从所有已知的 .pkgdef 文件添加到此存储区中。

  • 用户设置,是可写设置如在 选项 对话框的页中,属性页和其他某些显示对话框。 Visual Studio 扩展可以为的本地存储区使用这些数据。

本演练演示如何使用两组存储。

创建示例项目

本节演示如何创建基本设置中存储项。

创建示例项目

  1. 创建一 Visual Studio 包 " 项目命名 SettingsStorage。

    有关如何创建托管 VSPackage 的更多信息,请参见 演练:使用 Visual Studio 创建包模板的菜单命令

  2. 选择一种编程语言 页上,选择 Visual C#

  3. 选择 VSPackage 选项 页上,选择 菜单命令

  4. 命令选项 页,请将 命令名 到 设置存储 和 命令 ID 到 cmdidSettingsStorage。

  5. 选择测试选项 页中,清除两个复选框然后单击 完成

  6. 按 F5 运行 Visual Studio 的实验实例。

  7. 在的实验实例中,在 工具 菜单上,单击 设置存储

    应将显示以下消息。

    内部 Microsoft.SettingsStorage.SettingsStoragePackage.MenuItemCallback ()。

  8. 关闭实验实例。

可通过多种方式来从 VSPackage 的输出文本。 本演练演示如何修改 SettingsStoragePackage.MenuItemCallback 方法显示以下操作的结果集步骤。

使用配置设置存储

本节演示如何使用各种 SettingsManagerSettingsStore 方法检测和显示配置设置。

使用配置设置存储

  1. 在代码编辑器中打开 SettingsStoragePackage.cs 文件。

  2. 将以下各行添加到文件的开头:

    using System.Collections.Generic;
    using Microsoft.VisualStudio.Settings;
    using Microsoft.VisualStudio.Shell.Settings;
    
  3. 将以下各行添加到 MenuItemCallback 方法的开头:

    SettingsManager settingsManager = new ShellSettingsManager(this);
    SettingsStore configurationSettingsStore = settingsManager.GetReadOnlySettingsStore(SettingsScope.Configuration);
    

    此通知 #ctor 构造函数,并且通过对 VSPackage 作为 IServiceProvider 参数。 构造函数将返回 SettingsManager 对象。 GetReadOnlySettingsStore 方法返回名为 ConfigurationSettingsStoreSettingsStore 对象。 使用此对象读取配置设置。

  4. 在上一步中的行之后添加以下行:

    bool isVisualWebDevInstalled = configurationSettingsStore.CollectionExists("InstalledProducts\\Visual Web Developer");
    string message = "Visual Web Developer Installed: " + isVisualWebDevInstalled;
    

    这样会搜索配置设置存储已 subcollection visual web developer 的集合 InstalledProducts。 如果找到 subcollection,消息字符串设置为 “true”;否则,它设置为 “False”。

  5. 在 MessageBox 用该名称 “消息”调用,替换参数值 “SettingsStorage”。 MenuItemCallback 方法现在应类似于:

    private void MenuItemCallback(object sender, EventArgs e)
    {
      SettingsManager settingsManager = new ShellSettingsManager(this);
      SettingsStore configurationSettingsStore = settingsManager.GetReadOnlySettingsStore(SettingsScope.Configuration);
    
      bool isVisualWebDevInstalled = configurationSettingsStore.CollectionExists("InstalledProducts\\Visual Web Developer");
      string message = "Visual Web Developer Installed: " + isVisualWebDevInstalled;
      IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
      Guid clsid = Guid.Empty;
      int result;
    
    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(0, ref clsid, message, 
    //was "SettingsStorage", 
    //string.Format(CultureInfo.CurrentCulture, 
    //"Inside {0}.MenuItemCallback()", 
    //this.ToString()), string.Empty, 0, 
    //OLEMSGBUTTON.OLEMSGBUTTON_OK,
    //OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, 
    //OLEMSGICON.OLEMSGICON_INFO, 0, 
    // false out result));
    }
    
  6. 按 F5 运行 Visual Studio 的实验实例。

  7. 在的实验实例中,在 工具 菜单上,单击 设置存储

    此时应显示消息和包含文本 trueFalse执行的 安装的 visual web developer:

  8. 关闭实验实例。

Visual Studio 实现系统注册表中设置存储区。 因此,您可以使用注册表编辑器,例如, Regedit.exe,验证配置和用户设置。

使用注册表编辑器验证配置设置

  1. 打开 Regedit.exe 或另一个注册表编辑器。

  2. 导航到 HKEY_CURRENT_USER \ software \ Microsoft \ VisualStudio \ 10.0Exp_Config \ InstalledProducts \。

    备注

    确保您查看包含 10.0Exp_Config \ \ 而不是 \ 10.0_Config \ 的键。当您运行 Visual Studio 的实验实例中,配置设置注册表项 “10.0Exp_Config”。

  3. 展开 \ 安装的产品 \ 节点。 如果在上一步中的消息为 安装的 visual web developer:true,则 \ 安装的产品 \ 应包含一个 visual web developer 节点。 如果消息为 安装的 visual web developer:False,则 \ 安装的产品 \ 不应包含一个 visual web developer 节点。

还可以使用 CollectionExists 方法确定是否安装了特定的服务。 您必须知道服务类的类型。

确定服务是否可用

  1. 在代码中,请用以下行替换 IsVisualWebDevInstalled 和消息赋值语句。

    string helpServiceGUID = typeof(SVsHelpService).GUID.ToString("B").ToUpper();
    bool hasHelpService = configurationSettingsStore.CollectionExists("Services\\" + helpServiceGUID);
    string message = "Help Service Available: " + hasHelpService;
    

    该搜索具有服务的 GUID 名为的 subcollection 的配置设置存储集合服务。 如果找到 subcollection,消息字符串设置为 “true”;否则,它设置为 “False”。

  2. 按 F5 运行 Visual Studio 的实验实例。

  3. 在的实验实例中,在 工具 菜单上,单击 设置存储

    此时应显示消息和包含文本 trueFalse执行的 提供帮助的服务: 。 如前面步骤中所示,若要验证此设置,可以使用注册表编辑器,。

  4. 关闭实验实例。

还可以使用 GetSubCollectionNamesGetString 方法列出了配置设置存储注册的服务。

列出可用的服务

  1. 用以下行替换上一步中的代码。

    string message = "First five services:\n";
    IEnumerable<string> collection = configurationSettingsStore.GetSubCollectionNames("Services");
    int n = 0;
    foreach (string service in collection)
    {
        if (n++ == 5) break;
        message += configurationSettingsStore.GetString("Services\\" + service, "Name", "Unknown") + "\n";
    }
    

    GetSubCollectionNames 调用方法获取服务集合的成员的名称。 GetString 方法调用返回每个成员 Name 属性数据。 如果 Name 属性不存在,默认名称 “未知”返回。 生成的名称追加到消息字符串。

  2. 按 F5 运行 Visual Studio 的实验实例。

  3. 在的实验实例中,在 工具 菜单上,单击 设置存储

    此时应显示消息和包含以下文本。

    前五个服务:对于TypeSystemMicrosoft Visual Basic 编译器STestExecutionServiceResX RefactorNotify 服务

    您的服务列表可能不同。 若要验证这些设置,您可以使用注册表编辑器。

  4. 关闭实验实例。

通过使用以下过程,可以重置配置设置。

重置配置设置

  1. 单击 启动,单击 所有程序,单击 Microsoft Visual Studio 2010 SDK,单击 工具,然后单击 重置 Microsoft Visual Studio 2010 的实验实例

    命令将出现窗口。

  2. 请等待,直到 Press any key to continue 返回,然后按键盘键关闭命令窗口。

使用用户设置存储

本节演示如何添加记事本到 Visual Studio 为外部工具通过读取和写入用户设置存储区。

因此您可以调试和重复此安装,您必须能够重置外部工具设置。 为此,必须保存原始设置,以便可以还原它们根据要求。

保存外部工具设置

  1. 打开 Regedit.exe 或另一个注册表编辑器。

  2. 导航到 HKEY_CURRENT_USER \ software \ Microsoft \ VisualStudio \ 10.0Exp \ external tools \。

    备注

    确保您查看包含 \ 10.0Exp \ 而不是 \ 10.0 \ 的键。当您运行 Visual Studio 的实验实例中,您的用户设置注册表项 “10.0Exp”。

  3. 右击 \ 外部工具 \ 子级,然后单击 导出。 确保 所选分支 中选择。

  4. 保存时的外部 Tools.reg 文件。

还原外部工具设置

  1. 右击 \ 外部工具 \ 注册表子项,然后单击 删除

  2. 确认项删除 对话框出现时,单击

  3. 右击外部 Tools.reg 文件,单击 打开。,然后单击 注册表编辑器

各种 SettingsManagerWritableSettingsStore 方法用于检测和修改用户设置。

使用用户设置存储

  1. 在代码编辑器中打开 SettingsStoragePackage.cs 文件。

  2. MenuItemCallback 方法中,添加此行代码在 ConfigurationSettingsStore 赋值语句的后面:

    WritableSettingsStore userSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
    

    GetWritableSettingsStore 方法调用返回 WritableSettingsStore 个名为的对象 userSettingsStore。 您将使用此对象读写用户设置。

  3. 在上一步中的行之后,添加以下行:

    int toolCount = userSettingsStore.GetInt32("External Tools", "ToolNumKeys");
    

    GetInt32 方法调用用户为该集合外部工具存储并返回 ToolNumKeys 属性数据为整数的搜索。 这是已安装的外部工具的数目。

  4. 在上一步中的行之后,添加以下各行:

    bool hasNotepad = false;
    CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;
    for (int i = 0; i < toolCount; i++)
    {
        if (Compare.IndexOf(userSettingsStore.GetString("External Tools", "ToolCmd" + i), "Notepad", CompareOptions.IgnoreCase) >= 0)
        {
            hasNotepad = true;
            break;
        }
    }
    

    连续的 GetString 方法调用返回属性 ToolCmd0, ToolCmd1的数据,等等。 每个属性数据是外部工具的路径。 IndexOf 方法调用执行命令路径的不区分大小写的搜索字符串 “记事本”。 如果找到, hasNotepad 设置为 true;否则,将保持 false。

  5. 在上一步中的行之后,添加下面的行并替换消息字符串赋值语句,而其他语句。 foreach 结束循环。

    string message = (hasNotepad) ? "Notepad already installed" : "Installing Notepad";
    if (!hasNotepad)
    {
        userSettingsStore.SetString("External Tools", "ToolTitle" + toolCount, "&Notepad");
        userSettingsStore.SetString("External Tools", "ToolCmd" + toolCount, "C:\\Windows\\notepad.exe");
        userSettingsStore.SetString("External Tools", "ToolArg" + toolCount, "");
        userSettingsStore.SetString("External Tools", "ToolDir" + toolCount, "$(ProjectDir)");
        userSettingsStore.SetString("External Tools", "ToolSourceKey" + toolCount, "");
        userSettingsStore.SetUInt32("External Tools", "ToolOpt" + toolCount, 0x00000011);
    
         userSettingsStore.SetInt32("External Tools", "ToolNumKeys", toolCount + 1);
    }
    

    此代码使用 SetStringSetUInt32 方法创建安装记事本为外部工具的各个属性。 记事本提供访问键 “N”和项目目录作为开头的内容。 最后,外部工具数增加和写回用户设置存储区。

外部工具记事本为测试现在可用。

测试外部工具设置

  1. 按 F5 运行 Visual Studio 的实验实例。

  2. 在的实验实例中,在 工具 菜单上,单击 设置存储

    此时应显示消息和包含文本 安装记事本。 单击**“确定”**。

  3. 工具 菜单上,单击 记事本

    记事本打开。

  4. 在记事本打开后,请关闭它。

  5. 工具 菜单上,单击重新 记事本

    此时应显示消息和包含文本 已安装的记事本

  6. 关闭 " 记事本 ",然后关闭实验实例。

若要重复此测试,首先还原受影响的用户设置,如前面所述在 “还原外部工具设置”。

使用从其他的设置存储过程

首先显示此演练演示如何创建设置管理器 (SettingsManager 对象),使用 VSPackage 作为服务提供程序。

SettingsManager settingsManager = new ShellSettingsManager(this);

Vspackage 运行在同一进程为 Visual Studio,但是,您还可以使用 ExternalSettingsManager 类中,设置从另一个存储过程的访问。

若要创建管理器从另一个进程的设置,调用 CreateForApplication 方法,并向其传递可执行的 Visual Studio 的完整路径 (devenv.exe)。 由于 Visual Studio 没有预设的位置,您必须检查系统注册表搜索路径。 此信息位于 HKLM \ SOFTWARE \ Microsoft \ VisualStudio \版本\ InstallDir \ 子级。

例如,访问设置为 Visual Studio 中存储 2010 中,检查 HKLM \ SOFTWARE \ Microsoft \ VisualStudio \ 10.0 \ InstallDir \。

备注

对于 windows 64 位操作系统,此子是 HKLM \ SOFTWARE \ Wow6432Node \ Microsoft \ VisualStudio \ 10.0 \ InstallDir \。

Visual Studio 2010 \ InstallDir \ 属性的数据通常为 C:\Program Files\Microsoft Visual Studio 10.0 \ Common7 \ IDE \。

备注

为 windows 64 位操作系统,此数据是通常为 C:\Program files (x86) \ Microsoft Visual Studio 10.0 \ Common7 \ IDE \。

,例如,完整路径是通过追加到此数据的 devenv.exe 创建的

string AppIDPath = "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe"

最后,调用 CreateForApplication 创建设置管理器。

SettingsManager settingsManager = ExternalSettingsManager.CreateForApplication(AppIDPath);

请参见

其他资源

VSPackage 状态