Skip to main content

Crate win_desktop_utils

Crate win_desktop_utils 

Source
Expand description

Windows desktop helpers for Rust apps.

win-desktop-utils wraps a focused set of Windows desktop chores that many GUI, tray, installer-adjacent, and local utility apps need, while keeping raw Win32 shell, shortcut, mutex, known-folder, and elevation APIs out of the application code that uses them.

The low-level helpers stay available as small functions, and DesktopApp keeps common app startup and app-data setup in one place. On non-Windows targets, the public API still compiles and operational helpers return Error::Unsupported.

§When To Use This

NeedStart withFeature
App identity, app-data paths, and one-instance startupDesktopAppapp
Per-user local or roaming app-data directoriesensure_local_app_data or ensure_roaming_app_datapaths
Current-session single-instance behaviorsingle_instanceinstance
Global or builder-style single-instance behaviorsingle_instance_with_scope or SingleInstanceOptionsinstance
Open files, folders, URLs, Properties, or print verbsopen_with_default, open_url, or open_with_verbshell
Select a path in Explorerreveal_in_explorershell
Move files or folders to the Recycle Binmove_to_recycle_bin or move_paths_to_recycle_binrecycle-bin
Create .lnk or .url shortcutscreate_shortcut or create_url_shortcutshortcuts
Check or request administrator elevationis_elevated, restart_as_admin, or run_as_adminelevation

This crate is Windows-first. Non-Windows builds provide stubs so cross-platform applications can depend on the crate without wrapping the dependency itself in cfg(windows).

§When Not To Use This

Use the windows crate directly when you need broad Win32 coverage, custom flags, direct COM object ownership, or APIs outside this crate’s small desktop-app scope. Use a GUI framework when you need windows, controls, rendering, menus, or native widgets.

§Quick Start

fn main() -> Result<(), win_desktop_utils::Error> {
    let app = win_desktop_utils::DesktopApp::new(format!(
        "demo-app-{}",
        std::process::id()
    ))?;

    let _guard = match app.single_instance()? {
        Some(guard) => guard,
        None => {
            println!("already running");
            return Ok(());
        }
    };

    let local = app.ensure_local_data_dir()?;
    assert!(local.exists());

    Ok(())
}

§Feature Flags

Default features enable the full API. Consumers can opt out and select only the modules they need:

[dependencies]
win-desktop-utils = { version = "0.5", default-features = false, features = ["paths", "instance"] }

Cross-platform applications can also keep the dependency Windows-only:

[target.'cfg(windows)'.dependencies]
win-desktop-utils = "0.5"

Available features:

  • app: DesktopApp facade for app-data and single-instance startup.
  • paths: per-user local and roaming app-data helpers.
  • instance: named-mutex single-instance helpers.
  • shell: shell opening, URL, Explorer, and shell-verb helpers.
  • recycle-bin: Recycle Bin move and empty helpers.
  • shortcuts: .lnk and .url shortcut helpers.
  • elevation: elevation detection and shell-based relaunch helpers.

The default feature set is intentionally broad for convenience. Feature flags control this crate’s public modules; the underlying windows dependency uses one shared set of Win32 bindings when any Windows API feature is enabled. CI checks no-default, each individual public feature, and every pairwise public feature combination.

§Common Workflows

Startup guard plus app-data directory:

let app = win_desktop_utils::DesktopApp::new(format!(
    "workflow-demo-{}",
    std::process::id()
))?;
let _guard = app.single_instance()?.expect("first instance");
let config_dir = app.ensure_local_data_dir()?;
assert!(config_dir.exists());

Create a shortcut:

let shortcut = std::env::current_dir()?.join("notepad.lnk");
let options = win_desktop_utils::ShortcutOptions::new()
    .description("Open Notepad");
win_desktop_utils::create_shortcut(&shortcut, r"C:\Windows\notepad.exe", &options)?;

Relaunch the current executable as administrator:

use std::ffi::OsString;

if !win_desktop_utils::is_elevated()? {
    win_desktop_utils::restart_as_admin(&[OsString::from("--elevated")])?;
}

§Behavior And Side Effects

§Runtime Model

The crate does not start a background runtime, async executor, telemetry, or global worker. Most helpers validate input and then delegate to Windows. The shortcut and Recycle Bin helpers use short-lived STA worker threads for COM operations that require apartment-threaded shell APIs.

§Stability

The minimum supported Rust version is 1.82. Public API compatibility is checked in CI with cargo-semver-checks, and dependency policy is checked with cargo-deny.

Re-exports§

pub use error::Error;
pub use error::Result;
pub use app::DesktopApp;
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§

app
App-startup facade for identity, app-data paths, and single-instance guards.
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 .lnk and .url files.