Expand description
Windows-first desktop utility helpers for Rust apps.
win-desktop-utils provides small, focused helpers for common Windows desktop-app
tasks without forcing consumers to work directly with raw Win32 shell, mutex, and
known-folder APIs.
§Scope
This crate currently provides helpers for:
- opening files and directories with the default shell handler
- opening files and directories with explicit shell verbs
- opening URLs
- revealing items in Explorer
- sending files or directories to the Recycle Bin
- creating Windows
.lnkand.urlshortcuts - enforcing single-instance behavior
- resolving per-user app-data directories
- creating per-user app-data directories if needed
- checking elevation and relaunching as admin
This crate is intended for Windows desktop applications and utilities. Some functions launch external shell behavior or may trigger a UAC prompt. This crate supports Windows only.
§Current API
open_with_defaultopen_with_verbshow_propertiesprint_with_defaultopen_urlreveal_in_exploreropen_containing_foldermove_to_recycle_binmove_paths_to_recycle_binempty_recycle_binempty_recycle_bin_for_rootcreate_shortcutcreate_url_shortcutsingle_instancesingle_instance_with_scopesingle_instance_with_optionsroaming_app_datalocal_app_dataensure_roaming_app_dataensure_local_app_datais_elevatedrestart_as_adminrun_as_adminrun_with_verbInstanceScopeSingleInstanceOptionsShortcutOptionsShortcutIcon
§Example
fn main() -> Result<(), win_desktop_utils::Error> {
let _guard = match win_desktop_utils::single_instance("demo-app")? {
Some(guard) => guard,
None => {
println!("already running");
return Ok(());
}
};
let local = win_desktop_utils::ensure_local_app_data("demo-app")?;
assert!(local.ends_with("demo-app"));
Ok(())
}§Behavior Notes
open_with_defaultrequires a non-empty existing path.open_with_verbusesShellExecuteWwith the requested shell verb.show_propertiesandprint_with_defaultare small shell-verb wrappers.open_urltrims surrounding whitespace before delegating to the Windows shell.reveal_in_explorerrequires an existing path and launchesexplorer.exe.open_containing_folderopens the existing path’s parent directory.move_to_recycle_binrequires an absolute existing path and usesIFileOperationon a dedicated STA thread for recycle-bin behavior.move_paths_to_recycle_binvalidates all paths before starting one shell recycle-bin operation.empty_recycle_binpermanently empties the Recycle Bin without showing shell UI.create_shortcutusesIShellLinkWon a dedicated STA thread.roaming_app_dataandlocal_app_dataresolve their base directories viaSHGetKnownFolderPath.single_instanceuses aLocal\...named mutex, so the lock is scoped to the current Windows session, andapp_idcannot contain backslashes.single_instance_with_scopecan opt into either the current-session or global mutex namespace.restart_as_adminstarts a new elevated instance of the current executable, does not terminate the current process, and rejects arguments containing NUL bytes.run_as_adminandrun_with_verblaunch arbitrary commands throughShellExecuteW.
Re-exports§
pub use error::Error;pub use error::Result;pub use elevation::is_elevated;pub use elevation::restart_as_admin;pub use elevation::run_as_admin;pub use elevation::run_with_verb;pub use instance::single_instance;pub use instance::single_instance_with_options;pub use instance::single_instance_with_scope;pub use instance::InstanceGuard;pub use instance::InstanceScope;pub use instance::SingleInstanceOptions;pub use paths::ensure_local_app_data;pub use paths::ensure_roaming_app_data;pub use paths::local_app_data;pub use paths::roaming_app_data;pub use shell::empty_recycle_bin;pub use shell::empty_recycle_bin_for_root;pub use shell::move_paths_to_recycle_bin;pub use shell::move_to_recycle_bin;pub use shell::open_containing_folder;pub use shell::open_url;pub use shell::open_with_default;pub use shell::open_with_verb;pub use shell::print_with_default;pub use shell::reveal_in_explorer;pub use shell::show_properties;pub use shortcuts::create_shortcut;pub use shortcuts::create_url_shortcut;pub use shortcuts::ShortcutIcon;pub use shortcuts::ShortcutOptions;
Modules§
- elevation
- Elevation helpers for checking admin state and relaunching through UAC.
- error
- Shared error and result types for the crate.
- instance
- Single-instance helpers backed by named Windows mutexes.
- paths
- Helpers for resolving and creating per-user application data directories.
- shell
- Shell-facing helpers for opening files, URLs, Explorer selections, and the Recycle Bin.
- shortcuts
- Shortcut helpers for Windows
.lnkand.urlfiles.