由于毕业设计项目需要删除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"可以打开最近浏览文件和文件夹如下图所示:

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

当我们浏览文件时,它会自动的以快捷的方式存储历史记录,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快捷键的原始存放路径.

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

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

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.IO;              //Directory 目录   
  7. using System.Reflection;      //BindingFlags 枚举  
  8.   
  9. namespace GetPathRecent  
  10. {  
  11.     public class RecentlyFileHelper  
  12.     {  
  13.         public static string GetShortcutTargetFile(string shortcutFilename)  
  14.         {  
  15.             var type = Type.GetTypeFromProgID("WScript.Shell");  //获取WScript.Shell类型  
  16.             object instance = Activator.CreateInstance(type);    //创建该类型实例  
  17.             var result = type.InvokeMember("CreateShortCut", BindingFlags.InvokeMethod, null, instance, new object[] { shortcutFilename });  
  18.             var targetFile = result.GetType().InvokeMember("TargetPath", BindingFlags.GetProperty, null, result, nullas string;  
  19.             return targetFile;  
  20.         }  
  21.   
  22.         public static IEnumerable<string> GetRecentlyFiles()  
  23.         {  
  24.             var recentFolder = Environment.GetFolderPath(Environment.SpecialFolder.Recent);  //获取Recent路径  
  25.             return from file in Directory.EnumerateFiles(recentFolder)  
  26.                    where Path.GetExtension(file) == ".lnk"  
  27.                    select GetShortcutTargetFile(file);  
  28.         }  
  29.     }  
  30. }  

三.显示路径listBox1控件中

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

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace GetPathRecent  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         //载入Form   
  21.         private void Form1_Load(object sender, EventArgs e)  
  22.         {  
  23.             listBox1.Items.Clear();              
  24.             foreach (var file in RecentlyFileHelper.GetRecentlyFiles())  
  25.             {  
  26.                 listBox1.Items.Add(file);  
  27.             }  
  28.   
  29.             //获取recent路径  
  30.             var recentFolder = Environment.GetFolderPath(Environment.SpecialFolder.Recent);  
  31.             fileSystemWatcher1.Path = recentFolder;  
  32.             fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(fileSystemWatcher1_Created);  
  33.         }  
  34.   
  35.         //当在指定Path(即recent路径)中创建文件和目录时增加ShortCut  
  36.         private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)  
  37.         {  
  38.             listBox1.Items.Add(RecentlyFileHelper.GetShortcutTargetFile(e.FullPath));  
  39.         }  
  40.     }  
  41. }  

四.运行结果

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

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

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

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

五.总结及感谢

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-15
  • 2022-12-23
猜你喜欢
  • 2021-07-09
  • 2021-12-03
  • 2021-12-23
  • 2021-10-05
相关资源
相似解决方案