Hellagur
Hellagur

Reputation: 175

Is there someway to access the windows recent folder with rust?

  1. Try to write a rust script to remove some recent opened files on windows.
  2. Be aware that they are in "C:\Users\xx\Recent" folder.
  3. Be able to see them with Win+R then typing recent jumping to the recent folder.
  4. Try directly access to the folder with this code.
  5. Failed with error Os { code: 5, kind: PermissionDenied }.
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

  1. Is there some way to get permissions to access the recent folder?

    I have checked the windows-permission crate, but seems not help.

  2. 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

  3. 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

Answers (1)

Hellagur
Hellagur

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

Related Questions