有没有办法让程序在内存中打开时覆盖自身?

Is there a way for a program to write over itself while open in memory?

错误:

The process cannot access the file because it is being used by another process. (os error 32)

我正在用 Rust 创建一个程序,目前正在开发 'auto updater'。

如果发现更新,我试图让程序覆盖自身,然后在退出内存之前启动自身的更新版本(永久替换为更新版本)。当然,如果没有更新的版本,它将继续正常运行。

我目前的解决方法是在“启动器”中安装更新程序,在启动程序之前进行检查和更新。这样它就可以覆盖主程序,因为它当前没有在内存中打开。但是,如果只有一个可执行文件会好很多。

一些代码:

// Installs the update - writing over the currently installed version.
fs::write(env::current_dir().unwrap().join("program.exe"), program).unwrap();

一种更安全的方法是让您的主应用程序成为可执行文件的符号link,而不是可执行文件本身。然后将新版本下载到并行位置,将 link 切换到新版本,然后删除旧版本。