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

Win11下使用C#程序化固定/取消固定快捷方式

Windows 11下用.NET Framework 4.8实现应用固定到开始菜单和任务栏的方案

可以实现,Windows 11虽调整了开始菜单底层结构,但通过COM调用Shell组件的方式,依然能完成固定/取消固定操作,且完全兼容.NET Framework 4.8。以下是具体实现方案:

核心原理

通过调用Windows Shell的Shell.Application COM组件,获取应用程序文件的右键操作动词(Verb),执行对应的固定/取消固定命令。这种方式和用户手动右键点击操作的逻辑一致,兼容性较强。

代码实现

通用说明

  • 无需额外安装第三方库,依赖.NET Framework的COM互操作能力
  • 系统目录下的应用可能需要管理员权限才能执行操作
  • 使用CommandID判断操作动词,避免系统语言差异导致的识别失败

固定/取消固定到任务栏

using System.IO;

public static class TaskbarPinner
{
    // 固定到任务栏(CommandID: 4702)
    public static bool PinToTaskbar(string exePath)
    {
        if (!File.Exists(exePath))
            throw new FileNotFoundException("应用程序路径不存在", exePath);

        return ExecuteShellVerb(exePath, 4702);
    }

    // 从任务栏取消固定(CommandID: 4703)
    public static bool UnpinFromTaskbar(string exePath)
    {
        if (!File.Exists(exePath))
            throw new FileNotFoundException("应用程序路径不存在", exePath);

        return ExecuteShellVerb(exePath, 4703);
    }

    private static bool ExecuteShellVerb(string filePath, int commandId)
    {
        var shellType = Type.GetTypeFromProgID("Shell.Application");
        dynamic shell = Activator.CreateInstance(shellType);
        dynamic folder = shell.Namespace(Path.GetDirectoryName(filePath));
        dynamic fileItem = folder.ParseName(Path.GetFileName(filePath));

        foreach (dynamic verb in fileItem.Verbs())
        {
            if (verb.CommandID == commandId)
            {
                verb.DoIt();
                return true;
            }
        }

        return false; // 已执行对应操作或不支持该操作
    }
}

固定/取消固定到开始菜单

public static class StartMenuPinner
{
    // 固定到开始菜单(CommandID: 5386)
    public static bool PinToStartMenu(string exePath)
    {
        if (!File.Exists(exePath))
            throw new FileNotFoundException("应用程序路径不存在", exePath);

        return ExecuteShellVerb(exePath, 5386);
    }

    // 从开始菜单取消固定(CommandID: 5387)
    public static bool UnpinFromStartMenu(string exePath)
    {
        if (!File.Exists(exePath))
            throw new FileNotFoundException("应用程序路径不存在", exePath);

        return ExecuteShellVerb(exePath, 5387);
    }

    private static bool ExecuteShellVerb(string filePath, int commandId)
    {
        var shellType = Type.GetTypeFromProgID("Shell.Application");
        dynamic shell = Activator.CreateInstance(shellType);
        dynamic folder = shell.Namespace(Path.GetDirectoryName(filePath));
        dynamic fileItem = folder.ParseName(Path.GetFileName(filePath));

        foreach (dynamic verb in fileItem.Verbs())
        {
            if (verb.CommandID == commandId)
            {
                verb.DoIt();
                return true;
            }
        }

        return false; // 已执行对应操作或不支持该操作
    }
}

使用示例

// 固定记事本到任务栏
TaskbarPinner.PinToTaskbar(@"C:\Windows\notepad.exe");

// 从任务栏取消固定记事本
TaskbarPinner.UnpinFromTaskbar(@"C:\Windows\notepad.exe");

// 固定记事本到开始菜单
StartMenuPinner.PinToStartMenu(@"C:\Windows\notepad.exe");

// 从开始菜单取消固定记事本
StartMenuPinner.UnpinFromStartMenu(@"C:\Windows\notepad.exe");

注意事项

  1. 若应用已处于固定状态,对应的取消固定动词才会出现,反之亦然,方法会返回false表示无需操作
  2. 针对UWP应用,路径需指向其对应的快捷方式(通常在%ProgramData%\Microsoft\Windows\Start Menu\Programs目录下)
  3. 部分杀毒软件可能拦截此类操作,需确保程序拥有足够权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 04:50:41