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

如何在C# WPF应用中显示最近使用的程序?

实现步骤与代码示例

1. 修正注册表读取逻辑

Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs下的.exe子键中,每个键值以二进制格式存储程序路径,需正确解析这些二进制数据,同时要处理32/64位系统的注册表视图差异。

2. WPF数据绑定实现

使用ObservableCollection作为数据源,确保ListBox能自动更新;通过XAML绑定自定义列表项样式,展示程序名称与路径。

完整代码示例

MainWindow.xaml

<Window x:Class="RecentAppsDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="最近使用程序" Height="450" Width="300">
    <Grid>
        <ListBox x:Name="RecentAppsListBox" Margin="10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding AppName}" FontWeight="Bold"/>
                        <TextBlock Text="{Binding AppPath}" FontSize="10" Foreground="Gray"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

MainWindow.xaml.cs

using Microsoft.Win32;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;

namespace RecentAppsDemo
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<RecentApp> RecentApps { get; set; } = new ObservableCollection<RecentApp>();

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            LoadRecentApps();
        }

        private void LoadRecentApps()
        {
            RecentApps.Clear();
            var registryPath = @"Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs\.exe";

            // 自动适配32/64位系统注册表视图
            using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default);
            using var exeKey = baseKey.OpenSubKey(registryPath);

            if (exeKey == null)
            {
                MessageBox.Show("未找到最近使用的程序记录");
                return;
            }

            // 遍历并解析所有二进制存储的程序路径
            foreach (var valueName in exeKey.GetValueNames())
            {
                var binaryData = exeKey.GetValue(valueName) as byte[];
                if (binaryData == null) continue;

                // 二进制数据前2字节为路径长度,后续为UTF-16LE编码的路径字符串
                var path = Encoding.Unicode.GetString(binaryData, 2, binaryData.Length - 2).TrimEnd('\0');
                if (string.IsNullOrEmpty(path)) continue;

                // 从路径中提取程序名称
                var appName = System.IO.Path.GetFileNameWithoutExtension(path);
                RecentApps.Add(new RecentApp { AppName = appName, AppPath = path });
            }

            RecentAppsListBox.ItemsSource = RecentApps;
        }
    }

    // 数据模型类,用于绑定展示
    public class RecentApp
    {
        public string AppName { get; set; }
        public string AppPath { get; set; }
    }
}

关键说明

  • 注册表解析:RecentDocs\.exe下的二进制值,前2字节是路径长度标识,需跳过这部分后用UTF-16LE解码得到完整路径。
  • 注册表视图适配:RegistryView.Default会自动匹配系统位数,避免因注册表重定向导致读取不到对应位数程序的记录。
  • 数据绑定:ObservableCollection支持自动通知UI更新,配合DataTemplate可灵活定义列表项的展示样式。
  • 权限说明:当前用户权限即可读取该注册表路径,无需管理员权限。

若仍无法获取数据,可排查:

  • 系统是否禁用了最近使用记录功能
  • 程序是否以管理员身份运行(可能触发注册表重定向)

内容的提问来源于stack exchange,提问作者Witch Of Greed

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 14:35:29