Function known_folders::get_known_folder_path

source ·
pub fn get_known_folder_path(known_folder: KnownFolder) -> Option<PathBuf>
Expand description

Retrieve the full path of a known folder identified by the folder’s KNOWNFOLDERID.

A safe wrapper around the SHGetKnownFolderPath Win32 API function on Windows.

See KnownFolder for the types of known folders this function can retrieve.

§Errors

If an error occurs when calling the underlying Windows APIs or the given Known Folder ID is not present on the system (for example, if the ID was introduced in a newer OS version), None is returned.

§Examples

use known_folders::{get_known_folder_path, KnownFolder};

let profile_dir = get_known_folder_path(KnownFolder::Profile);
Examples found in repository?
examples/get_profile_dir.rs (line 98)
97
98
99
100
101
102
103
104
105
106
107
    pub fn try_main() -> Result<(), Box<dyn Error>> {
        let profile_dir = get_known_folder_path(KnownFolder::Profile).ok_or(PlatformError)?;

        let display = profile_dir
            .into_os_string()
            .into_string()
            .map_err(|_| UnsupportedPathEncoding)?;

        writeln!(io::stdout(), "Profile directory: {display}")?;
        Ok(())
    }