# Why can't my Windows app overwrite its own .exe when it updates itself?
Source: https://ruststudio.org/community/t/269/why-can-t-my-windows-app-overwrite-its-own-exe-when-it-updates-itself
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11; compiled and run. The program copies its own executable to a temporary folder, runs the copy, and tries to overwrite and rename the running copy. The rename result is what was observed on this machine, not a general guarantee.
Category: Help and questions | Kind: FAQ | Tags: auto-update, windows, files, executable | Views: 0

## Short answer
Windows keeps the executable of a running program open, so writing to it fails with error 32, used by another process. Renaming it worked on the test machine. An updater can rename the running file aside and write the new one, or hand the job to a helper after the app exits. The real error is shown.

## Answer
## Answer
On Windows the file of a running program is held open by the system. Replacing it with a new version while it runs is refused. This program shows it: it copies itself to a temporary folder, starts the copy (which just sleeps for three seconds), then tries to write to and to rename the running copy.

```rust title=src/main.rs
use std::fs;
use std::process::Command;
use std::time::Duration;

fn main() -> std::io::Result<()> {
    if std::env::args().nth(1).as_deref() == Some("sleep") {
        std::thread::sleep(Duration::from_secs(3)); // this copy plays "the running application"
        return Ok(());
    }
    let me = std::env::current_exe()?;
    let dir = std::env::temp_dir().join("rs-forum-update-demo");
    fs::create_dir_all(&dir)?;
    let running = dir.join("app.exe");
    fs::copy(&me, &running)?;
    let mut child = Command::new(&running).arg("sleep").spawn()?;
    std::thread::sleep(Duration::from_millis(500));

    match fs::write(&running, b"new version") {
        Ok(()) => println!("overwrite: ok"),
        Err(e) => println!("overwrite: {e}"),
    }
    match fs::rename(&running, dir.join("app.old")) {
        Ok(()) => println!("rename: ok"),
        Err(e) => println!("rename: {e}"),
    }
    fs::write(&running, b"new version")?; // the name is free now
    println!("new file written under the old name");
    child.wait()?;
    fs::remove_dir_all(&dir)
}
```

```text
overwrite: The process cannot access the file because it is being used by another process. (os error 32)
rename: ok
new file written under the old name
```

So on this Windows 11 machine:

1. **Overwriting** the running file failed with error 32.
2. **Renaming** it succeeded, which frees the original name. A new file can then be written there, and the running program keeps working from the renamed (old) file.

Designs that follow from it:

- **Rename, write, restart.** The app renames `app.exe` to `app.old`, writes the new `app.exe`, starts it and exits. On the next start, delete `app.old` (it cannot be deleted while the old process runs).
- **Helper process.** The app starts a small updater (a separate executable) and exits. The updater waits for the app's process to end, replaces the files, and starts the new version. This also handles updates of several files.
- **An installer does it.** Start the downloaded installer and exit, so installation happens when the app is not running.

Whichever you pick, verify the new file before you replace anything (see the update approaches thread), and keep the old one until the new one has started successfully.

## Common mistakes
- Replacing the file without a signature check on the download.
- Deleting `app.old` immediately while its process still runs.
- Not handling the failure. If the rename fails (a virus scanner or another program holds the file), tell the user and keep running the old version.

## Related questions
### Is it the same on macOS?
This thread was run on Windows only and makes no claim about macOS. Apple's documentation for update frameworks such as Sparkle describes that platform's approach.

## Sources
- std::fs::rename: https://doc.rust-lang.org/std/fs/fn.rename.html (read 2026-12-05)
- Microsoft, System error codes (32, ERROR_SHARING_VIOLATION): https://learn.microsoft.com/windows/win32/debug/system-error-codes--0-499- (read 2026-12-05)
