vb.net 中的自更新应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17334298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:03:05  来源:igfitidea点击:

Self updating application in vb.net

vb.netprocessexecutableself-updating

提问by Lucas

I want my application to self-update if there is a different size executable available on the remote server. The problem I got is that when I kill the process to replace the application executable, nothing more happends - nothing more is executing after the eprocess.Kill()even though I am trying to freeze the thread during the file replacement process. Is there something I am doing wrong?

如果远程服务器上有不同大小的可执行文件,我希望我的应用程序能够自我更新。我遇到的问题是,当我终止进程以替换应用程序可执行文件时,什么也没有发生 -eprocess.Kill()即使我在文件替换过程中试图冻结线程,也不会再执行任何操作。有什么我做错了吗?

Here is my code:

这是我的代码:

            Dim Request As System.Net.WebRequest
            Dim Response As System.Net.WebResponse
            Dim FileSize As Integer
            Request = Net.WebRequest.Create("http://mywebsite.com/File.exe")
            Request.Method = Net.WebRequestMethods.Http.Get
            Response = Request.GetResponse
            FileSize = Response.ContentLength
            Dim mySize As New IO.FileInfo(Application.ExecutablePath)
            If FileSize <> mySize.Length Then                    If File.Exists(tempPath & "\File_tmp.exe") Then
                    File.Delete(tempPath & "\File_tmp.exe")
                End If
                Patcher.DownloadFileAsync(New Uri("http://mywebsite.com/File.exe"), tempPath & "\File_tmp.exe") 'Patcher is defined before, you might think that its not working, but this is just a piece of code, and the new file is downloading properly. The described problem is 
                While Patcher.IsBusy
                    Threading.Thread.Sleep(100)
                End While

                Do While True
                    For Each eprocess As Process In Process.GetProcesses
                        If eprocess.ProcessName = "MyApplication" Then
                            eprocess.Kill()
                        End If
                    Next
                    File.Delete(Application.ExecutablePath)
                    'Copy downloaded file to the application executable path directory
                    File.Copy(tempPath & "\File_tmp.exe", Application.ExecutablePath)
                    Threading.Thread.Sleep(30) 'freeze thread...
                Loop
            End If

回答by OneFineDay

One way to handle this is make a separate(smaller exe) in your project that handles the downloading of the new version. First it would close the current version and since it's not tied to the original app the uploader app is still running, it downloads, then installs it and then launches the new version.

处理此问题的一种方法是在您的项目中创建一个单独的(较小的 exe)来处理新版本的下载。首先它会关闭当前版本,因为它没有绑定到原始应用程序,上传器应用程序仍在运行,它会下载,然后安装它,然后启动新版本。

回答by Matt Wilko

There is built in functionality to do this for you.

有内置功能可以为您执行此操作。

Have a look at ClickOnce. This negates the need to have a separate application to do the updating for you and allows you to specify a minimum and recommended version, whether the app checks for a new version before or after it starts, etc.

看看ClickOnce。这不需要有单独的应用程序来为您进行更新,并允许您指定最低和推荐版本,应用程序是否在启动之前或之后检查新版本等。

Some more links that may be of use:

还有一些可能有用的链接:

http://msdn.microsoft.com/en-us/magazine/cc163973.aspxhttp://www.codeproject.com/Articles/38546/Click-Once-Deployment-Technique

http://msdn.microsoft.com/en-us/magazine/cc163973.aspx http://www.codeproject.com/Articles/38546/Click-Once-Deployment-Technique

回答by Angry 84

Thought i would add my suggestion to the mix being that this is still the top result in google for stackoverflow.

我想我会添加我的建议,因为这仍然是 google 中 stackoverflow 的最佳结果。

Firstly.. I can not stand the ClickOnce Solution as it modifies to much of the project and requires other steps and so on.. So i ruled that out after 10 minutes of looking into it.

首先.. 我无法忍受 ClickOnce 解决方案,因为它修改了大部分项目并需要其他步骤等等.. 所以我在研究 10 分钟后排除了它。

Also as for having another exe to handle the updates..This was not ideal for me either... So i had my main exe/setup install do all this for me.

另外还有另一个 exe 来处理更新......这对我来说也不理想......所以我让我的主要 exe/setup 安装为我做这一切。

Basically i have a website where i host the setup install files with a simple JSON data file to hold the version information. This is accessed via the application itself using a http request and json decoding to query the latest version and compare it against the apps current version.

基本上我有一个网站,我用一个简单的 JSON 数据文件来托管设置安装文件来保存版本信息。这是通过应用程序本身访问的,使用 http 请求和 json 解码来查询最新版本并将其与应用程序当前版本进行比较。

If a new version is found, the application will download a copy of the new install and place it inside of the applications main directory in a temp folder/installers folder.

如果找到新版本,应用程序将下载新安装的副本并将其放置在应用程序主目录中的临时文件夹/安装程序文件夹中。

Once the download has completed, i execute the installer from within the application and dont waitforexit. The next like of code will trigger the applications save & exit functions.

下载完成后,我从应用程序中执行安装程序,不要等待退出。下一个类似的代码将触发应用程序的保存和退出功能。

At this point you have the install running but the main application has exited.. From here the installer can continue as normal and start the application on complete.

此时,您的安装正在运行,但主应用程序已退出。从这里安装程序可以正常继续并在完成时启动应用程序。

You can of course vary alot of things here like silent installs and so on.. but using this method, it allows me to get away with just the normal installer and app exe..

你当然可以在这里改变很多东西,比如静默安装等等。

回答by Mandolf0

I say you write a function that creates a batch file which

我说你写了一个函数来创建一个批处理文件

  • downloads the new version of your exe
  • closes your app and overwrites your exe with the new.
  • Opens your app
  • 下载您的 exe 的新版本
  • 关闭您的应用程序并用新文件覆盖您的 exe。
  • 打开您的应用

Then your app on startup should look for a such batch file and erase it.

然后你的应用程序在启动时应该寻找这样的批处理文件并删除它。