User Directories and Preferred Applications
Recipe | Crates | Categories |
---|---|---|
Locate User Directories with dirs | ||
Locate User Directories with directories | ||
Open a File or URL using the Program Configured on your System |
Locate User Directories
dirs
↗ is a tiny, low-level library with a minimal API that provides platform-specific standard locations of directories for configuration, cache and other data. It leverages the XDG base/user directory specifications on Linux, the "Known Folder" API on , and the Standard Directory guidelines on macOS.
The following example gets paths to standard (home, configuration, data, cache, executables) directories on the file system:
fn main() { // Get the home directory. if let Some(home_dir) = dirs::home_dir() { println!("Home directory: {home_dir:?}"); } else { println!("Home directory could not be found."); } // Get the configuration directory. if let Some(config_dir) = dirs::config_dir() { println!("Configuration directory: {config_dir:?}"); } else { println!("Configuration directory could not be found."); } // Get the data directory. if let Some(data_dir) = dirs::data_dir() { println!("Data directory: {data_dir:?}"); } else { println!("Data directory could not be found."); } // Get the cache directory. if let Some(cache_dir) = dirs::cache_dir() { println!("Cache directory: {cache_dir:?}"); } else { println!("Cache directory could not be found."); } // Get the executable directory. if let Some(exec_dir) = dirs::executable_dir() { println!("Executable directory: {exec_dir:?}"); } else { println!("Executable directory could not be found."); } }
Compute the Location of Cache, Config, or Data Directories for a Specific Application
directories
↗ is a mid-level library that provides platform-specific standard locations of directories for config, cache, and other data on Linux, Windows and macOS.
directories
↗ is higher-level than dirs
↗. Use directories
if you need to compute cache, config, etc. paths for specific applications or projects:
//! This example demonstrates how to use the `directories` crate to find the //! user's data and config directories for a specific application. use std::path::PathBuf; use anyhow::Result; use anyhow::anyhow; use directories::ProjectDirs; /// Returns the user's data directory. pub fn get_data_dir() -> Result<PathBuf> { let directory = if let Ok(s) = std::env::var("APP_DATA_DIR") { PathBuf::from(s) } else if let Some(proj_dirs) = ProjectDirs::from("com", "Foo Corp", "Bar App") { proj_dirs.data_local_dir().to_path_buf() } else { return Err(anyhow!("Unable to find the data directory")); }; Ok(directory) } /// Returns the user's config directory. pub fn get_config_dir() -> Result<PathBuf> { let directory = if let Ok(s) = std::env::var("APP_CONFIG_DIR") { PathBuf::from(s) } else if let Some(proj_dirs) = ProjectDirs::from("com", "Foo Corp", "Bar App") { proj_dirs.config_local_dir().to_path_buf() } else { return Err(anyhow!("Unable to find the config directory")); }; Ok(directory) } // Look into `BaseDirs` and `UserDirs` for additional directories as well. fn main() { println!("{:?}", get_data_dir()); println!("{:?}", get_config_dir()); }
Open a File or URL using the Program Configured on your System
open
↗ opens a path or URL using the default program configured on the system:
//! This example demonstrates how to use the `open` crate to open files and URLs //! using the default program associated with the file type. fn main() { // The `that` function attempts to open the specified path with the // appropriate application. If an error occurs during the opening // process, it will be printed to the standard error stream. if let Err(e) = open::that("example.txt") { eprintln!("Failed to open file: {e}"); } // Open a URL using the default web browser. if let Err(e) = open::that("https://www.rust-lang.org") { eprintln!("Failed to open URL: {e}"); } // OR: open::with("http://rust-lang.org", "firefox")?; // Depending on the platform and system configuration, launchers can block. // To be sure they don't, use `that_in_background()` or // `that_detached` instead. // Get the commands that would be used to try to open the specified path. // This is useful for understanding how the `open` crate would handle a // given path. The commands are printed to the standard output stream. let path = "http://rust-lang.org"; open::commands(path).iter().for_each(|x| println!("{x:?}")); }
Related Topics
- Directories.
- Paths.