You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2022中VSIX扩展:获取当前主题失败求助

检测Visual Studio当前主题的正确实现方式

你之前的代码取不到值,是因为从Visual Studio 2019开始,主题设置的存储路径发生了变化,不再位于General节点下的CurrentTheme键。以下是两种可行的实现方法:

方法一:通过IVsUIShell服务获取主题GUID

这是最可靠的方式,直接调用VS提供的UI Shell服务获取当前主题的唯一标识:

using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;

public string GetCurrentTheme()
{
    ThreadHelper.ThrowIfNotOnUIThread();
    var uiShell = ServiceProvider.GlobalProvider.GetService(typeof(SVsUIShell)) as IVsUIShell;
    if (uiShell == null)
        return string.Empty;

    uiShell.GetCurrentTheme(out Guid themeGuid);
    return themeGuid.ToString();
}

常见VS主题的GUID对应:

  • 浅色主题:de3dbbcd-f642-433c-8353-8f1df4370aba
  • 深色主题:1ded0138-47ce-435e-84ef-9ec1f439b749
  • 蓝色主题:a4d6a176-b948-4b29-8c66-53c97a1ed7d0

方法二:修改设置路径读取主题

如果坚持使用ShellSettingsManager,需要调整读取的节点和键名,正确的存储位置在FontsAndColors节点下:

var shellSettingsManager = new ShellSettingsManager(ServiceProvider.GlobalProvider);
var store = shellSettingsManager.GetReadOnlySettingsStore(SettingsScope.UserSettings);
var theme = store.GetString("FontsAndColors", "CurrentTheme", string.Empty);

返回的结果同样是主题的GUID字符串。

注意事项

  • 必须在UI线程调用这些API,通过ThreadHelper.ThrowIfNotOnUIThread()确保线程上下文正确。
  • 不同VS版本的主题GUID可能有更新,优先使用IVsUIShell.GetCurrentTheme方法获取最新值。

内容的提问来源于stack exchange,提问作者Tim Maes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 06:06:21