Reputation: 175
use dirs::home_dir;
use std::fs;
fn main() {
let user_folder = home_dir().unwrap();
let recent_folder = user_folder.join("Recent");
let recent_folder_contents = fs::read_dir(recent_folder.to_str().unwrap()).unwrap();
println!("{}", recent_folder.display());
for path in recent_folder_contents {
println!("{}", path.unwrap().path().display())
}
}
Then i want to know
Is there some way to get permissions to access the recent folder?
I have checked the windows-permission crate, but seems not help.
Did know that there's an api called SHAddToRecentDocs, which can add one or clear all recent files. Sadly not meet my requirements.
So is there any api can directly get the recent files? Or to enable/disable the option show recently used files in quick access.
Have searched the windows-rs crate but find nothing with keywords recent and shared
If using c#, seems to be easy with this code and without permission problem, not sure how to do this with rust.
DirectoryInfo d = new DirectoryInfo(
System.Environment.GetFolderPath(
Environment.SpecialFolder.Recent))
Upvotes: 2
Views: 325
Reputation: 175
Thanks for @AlexK.'s help. Now is the working code.
// cargo.toml
[dependencies]
walkdir = "2"
winsafe = { version = "0.0.11", features = [ "shell" ] }
// main.rs
use walkdir::WalkDir;
use winsafe::{co, SHGetKnownFolderPath};
fn main() {
let docs_folder = SHGetKnownFolderPath(
&co::KNOWNFOLDERID::Recent,
co::KF::DEFAULT,
None,
).unwrap();
println!("Recent Folder Path: {}", docs_folder);
for path in WalkDir::new(docs_folder) {
println!("{}", path.unwrap().path().display())
}
}
The recent files will be shown as xxx.automaticDestinations-ms or xxx.customDestinations-ms.
Still have some works to figure them out.
Upvotes: 2