[C#]如何取出最近在Windows上所使用的文件檔案
return targetFile;
}
public static IEnumerable<string> GetRecentlyFiles()
{
var recentFolder = Environment.GetFolderPath(Environment.SpecialFolder.Recent);
return from file in Directory.EnumerateFiles(recentFolder)
where Path.GetExtension(file) == ".lnk"
select GetShortcutTargetFile(file);
}
}</pre></div>
namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Clear();
foreach (var file in RecentlyFileHelper.GetRecentlyFiles())
{
listBox1.Items.Add(file);
}
var recentFolder = Environment.GetFolderPath(Environment.SpecialFolder.Recent);
fileSystemWatcher1.Path = recentFolder;
fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(fileSystemWatcher1_Created);
}
void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(RecentlyFileHelper.GetShortcutTargetFile(e.FullPath));
}
}
}