简体   繁体   English

vb.net 可执行文件可以替换自身吗?如何替换?

[英]vb.net Can an executable replace itself and how?

I want to make a simple updater for my program, but I do not want a separate executable.我想为我的程序制作一个简单的更新程序,但我不想要一个单独的可执行文件。 Is this possible?这可能吗?

So I check a location (ftp,http) for lets say version.txt, I compare versions.所以我检查一个位置(ftp,http)让我们说version.txt,我比较版本。 Now what?怎么办? Can I overwrite my running executable and how.我可以覆盖我正在运行的可执行文件以及如何覆盖。 My program is like 4 MB ...我的程序就像 4 MB ...

If it is not posible just tell me please :)如果不可行,请告诉我:)

Update:更新:

My.Computer.FileSystem.RenameFile("Test.exe", "SecondTest.exe")
My.Computer.FileSystem.CopyFile("12.exe", "Test.exe") 

This seems to work.这似乎有效。 So I rename my current executable, then put the other on its place ... Am I looking for trouble here?所以我重命名我当前的可执行文件,然后把另一个放在它的位置......我在这里找麻烦吗?

Ok so this works like a charm:好的,这就像一个魅力:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        My.Computer.FileSystem.RenameFile("Test.exe", "oldversion.exe")
        My.Computer.FileSystem.CopyFile("newversion.exe", "Test.exe")
        Process.Start("test.exe")
        End
    End Sub
End Class

So when test.exe runs, it renames itself to oldversion.exe then copies newversion to test.exe and starts it, then shuts down.因此,当 test.exe 运行时,它会将自身重命名为 oldversion.exe,然后将 newversion 复制到 test.exe 并启动它,然后关闭。

Now all I have to add is check for oldversion at every start and delete it if it exists.现在我要做的就是在每次启动时检查旧版本,如果存在则将其删除。 The trick was to rename itself ...诀窍是重命名自己......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM