C# 系统应用之获取Windows最近使用记录

由于毕业设计项目需要删除Windows最近历史记录,这就需要获取Windows最近历史记录 Recent.本文就主要叙述通过C#实现获取Recent中使用的文件和文件夹.首先声明该文章主要是结合自己的毕设项目,同时的主要代码来自Level Up的博客.在此非常感谢该博主,也希望大家学习他的文章: http://www.dotblogs.com.tw/larrynung/archive/2012/09/27/75118.aspx

一.Windows最近历史记录

在Windows系统中有Recent Items或Recent这样的东西存储最近使用的文件和文件夹的历史记录,通过快捷键"Windows+R"打开运行输入"recent"可以打开最近浏览文件和文件夹如下图所示:

当我们浏览文件时,它会自动的以快捷的方式存储历史记录,Windows会自动添加到该文件夹下记录系统最近使用的文件或文件夹,同样Office、Cookies等都有相对应的Recent.我们可以通过Environment.GetFolderPath(Environment.SpecialFolder.Recent)获取Windows的Recent最近历史记录的位置,我电脑中recent的路径为 "C:\Users\dell\AppData\Roaming\Microsoft\Windows\Recent".
同时由于该获取较简单,就不详细叙述.补充C#获取桌面、Recent、我的文档、我的音乐、Cookies等路径参考文章http://hi.baidu.com/ysuhy/item/b12a57d3660ccc90270ae7f9

二.遍历Recent中文件路径

在遍历Recent目录时,遍历方法参考了Level Up的文章"[C#][VB.NET].NET捷径(ShortCut)控制"如下图"dota2.txt"的捷径(ShortCut),通过目标获取Recent快捷键的原始存放路径.

这里Level Up作者整理了一个类,通过这个类实现获取文件原始路径,右键项目"添加"类,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;              //Directory 目录 
using System.Reflection;      //BindingFlags 枚举

namespace GetPathRecent
{
    public class RecentlyFileHelper
    {
        public static string GetShortcutTargetFile(string shortcutFilename)
        {
            var type = Type.GetTypeFromProgID("WScript.Shell");  //获取WScript.Shell类型
            object instance = Activator.CreateInstance(type);    //创建该类型实例
            var result = type.InvokeMember("CreateShortCut", BindingFlags.InvokeMethod, null, instance, new object[] { shortcutFilename });
            var targetFile = result.GetType().InvokeMember("TargetPath", BindingFlags.GetProperty, null, result, null) as string;
            return targetFile;
        }

        public static IEnumerable<string> GetRecentlyFiles()
        {
            var recentFolder = Environment.GetFolderPath(Environment.SpecialFolder.Recent);  //获取Recent路径
            return from file in Directory.EnumerateFiles(recentFolder)
                   where Path.GetExtension(file) == ".lnk"
                   select GetShortcutTargetFile(file);
        }
    }
}

三.显示路径listBox1控件中

向Form中添加控件listBox和fileSystemWatcher(监控文件系统更改通知,并在目录或文件更改时引发事件).具体代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GetPathRecent
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //载入Form 
        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Clear();            
            foreach (var file in RecentlyFileHelper.GetRecentlyFiles())
            {
                listBox1.Items.Add(file);
            }

            //获取recent路径
            var recentFolder = Environment.GetFolderPath(Environment.SpecialFolder.Recent);
            fileSystemWatcher1.Path = recentFolder;
            fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(fileSystemWatcher1_Created);
        }

        //当在指定Path(即recent路径)中创建文件和目录时增加ShortCut
        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(RecentlyFileHelper.GetShortcutTargetFile(e.FullPath));
        }
    }
}

四.运行结果

显示Recent中Windows最近浏览的文件及文件夹原始路径运行结果如下:

其中与"一.Windows历史记录"中图对应,亦可以发现dota2.txt是对应捷径"G:\dota2\dota2.txt"证明了文章.到此,我们就获取到了Windows的最近历史记录,要实现清除Recent的历史记录也非常容易,同时清除指定U盘中的文件记录也可以实现.同样如果想删除Office最近历史记录路径为"C:\Users\dell\AppData\Roaming\Microsoft\Office\Recent".如下图所示:

五.总结及感谢

该文章主要是结合自己的毕业设计中U盘清除Windows历史记录、Office历史记录设计完成,同时查看了很多资料和书籍,其中给予我帮助最大的是level up的文章,由于这方面的资料较少,所以弥足珍贵.同时声明该文章的代码主要参考了Level Up的博客http://www.dotblogs.com.tw/larrynung/archive/2012/09/27/75118.aspx
最后,希望文章对大家有所帮助,同时希望大家去关注上面提到博主的文章,他写了很多文章,都非常有用,无论是技术性还是理论性.如果该篇文章中有错误或不足之处,请大家海涵!
(By:Eastmount 2014-1-19 夜4点http://blog.csdn.net/eastmount)

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eastmount

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值