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

如何在VS 2022中获取当前Visual Studio主题ID

在Visual Studio 2022中获取当前主题ID的方法

Visual Studio 2022及后续版本已不再将主题ID存储在注册表中,需通过Visual Studio SDK提供的内部服务来获取,以下是两种可靠实现方式:

方法一:使用IVsUIShell2接口

适用于基于Visual Studio SDK的扩展项目,步骤如下:

  • 确保项目引用Microsoft.VisualStudio.Shell.Interop程序集
  • 通过ServiceProvider获取IVsUIShell2服务实例
  • 调用GetThemeIdentifier方法提取主题ID
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;

public string GetCurrentThemeId()
{
    string themeId = string.Empty;
    try
    {
        var serviceProvider = new ServiceProvider(AsyncPackage.GetGlobalService(typeof(SVsServiceProvider)) as IServiceProvider);
        if (serviceProvider != null)
        {
            var uiShell2 = serviceProvider.GetService(typeof(SVsUIShell)) as IVsUIShell2;
            if (uiShell2 != null)
            {
                uiShell2.GetThemeIdentifier(out Guid themeGuid);
                themeId = themeGuid.ToString("D");
            }
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine($"获取主题ID失败: {ex.Message}");
    }
    return themeId;
}

方法二:使用IVsThemeService(推荐)

从Visual Studio 2019起,官方推荐使用IVsThemeService获取主题信息,它提供更全面的主题操作能力:

  • 引用Microsoft.VisualStudio.Shell.Framework和Microsoft.VisualStudio.Shell.Interop程序集
  • 获取IVsThemeService实例
  • 通过CurrentTheme属性获取主题对象并提取ID
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;

public string GetCurrentThemeId()
{
    string themeId = string.Empty;
    try
    {
        var serviceProvider = new ServiceProvider(AsyncPackage.GetGlobalService(typeof(SVsServiceProvider)) as IServiceProvider);
        if (serviceProvider != null)
        {
            var themeService = serviceProvider.GetService(typeof(SVsThemeService)) as IVsThemeService;
            if (themeService != null)
            {
                var currentTheme = themeService.CurrentTheme;
                themeId = currentTheme.ThemeId.ToString("D");
            }
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine($"获取主题ID失败: {ex.Message}");
    }
    return themeId;
}

常见主题ID参考:

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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 13:21:11