【问题标题】:VB.NET - Auto-Updating applications while its open?VB.NET - 打开时自动更新应用程序?
【发布时间】:2016-06-03 21:04:48
【问题描述】:

我已将代码设置为使用Form1_Load 在运行时执行,该代码是一个简单的程序集检查,以查看版本是否低于通过 Webclient 获得的服务器上保存的版本。如果它较低,它会从那里下载最新的 EXE 并使用 CMD 重命名它并用旧的 .exe 替换它(在应用程序打开时它以某种方式工作)

问题:

  • 它使虚假反病毒检测认为它试图感染其他应用程序(Trojan Dropper Variant)

  • 在某些情况下无效(例如:用户第一次下载应用程序并将其重命名为默认 EXE 名称以外的其他名称,然后就无法正常工作)

  • CMD 甚至可能无法在某些机器上运行,例如 School PC 的 e.t.c

  • 一般来说,使用 CMD 并不是一个好主意。

我不想做的解决方案:

  • 使用第二个 .exe,其唯一目的是更新应用程序;

  • CMD 废话

  • 至少尽量不要使用来自 .dll 的 Nuget 包/外部引用

  • 不是仅当用户在 MessageBox e.t.c 上按 Yes/OK 时才更新的代码

那么我该如何解决这个不得不使用 CMD 的问题呢?

我当前的代码:

#Region "Update Checker"

#Region "Test Internet Connection"

    'Ping Google to see if the user has Internet Connected;
    Private Function TestInternetConnection() As Boolean
        Try
            Dim ping As New Net.NetworkInformation.Ping()
            ping.Send("google.com")
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

#End Region

#Region "Check for an Update"

    Private Sub CheckForUpdate()

        'If the user is Connected to the Internet;
        If TestInternetConnection() Then

            Try

                'Using WebClient, get the Newest File Version;
                Using wc As New WebClient

                    'Latest Version;
                    Dim LatestVersion As String = wc.DownloadString("http://proxyfuel.xyz/version.txt")

                    'If the Latest Version is Newer then the Current Version;
                    If LatestVersion > Application.ProductVersion Then

                        'Download the Latest Version of the EXE file;
                        wc.DownloadFile("{Application EXE Direct Link}", Application.StartupPath & "\update.exe")

                        'Execute the CMD Batch File to replace the old EXE file with the Newest EXE file;
                        Process.Start(New ProcessStartInfo("cmd.exe", String.Format("/k {0} & {1} & {2} & {3}", "ping -n 3 127.0.0.1 > NUL", "del ""ProxyFuel.exe""", "REN ""update.exe"" ""ProxyFuel.exe""", "start """" ""ProxyFuel.exe""")).WindowStyle = ProcessWindowStyle.Hidden)

                        'Close the Application;
                        Application.Exit()

                    Else

                        'Start Loading the Main Form;
                        My.Settings.updatechecked = True

                    End If

                End Using

            Catch ex As Exception

                'Updating had an Unexpected Error;
                MessageBox.Show(ex, "Update Error!", MsgBoxStyle.Critical)

            End Try

        Else

            'No Internet Connection - Couldnt connect to the Data Server;
            MessageBox.Show("Internet Connection Required!", "Error")

        End If

    End Sub

#End Region

#End Region

【问题讨论】:

  • (Somehow it works while the application is open) 不可能。也许它正在默默地失败。
  • @Plutonix 这是一种可能性,但因为它实际上并没有加载显示表单并在打开应用程序的同时关闭应用程序可能不是

标签: .net vb.net windows visual-studio cmd


【解决方案1】:

我有类似的情况需要更新当前正在运行的应用程序。我创建了一个中间应用程序来进行重命名(更新),这样主应用程序可以成功重命名,否则当主应用程序仍在运行时,文件将被锁定以进行大多数文件操作。

  1. 说,我的主应用程序是 [main],更新程序是 [updater]
  2. [main] 会检查是否有更新的版本
  3. [main] 将以不同的名称下载 [main-new]
  4. [main] 将运行 [updater] 并自行关闭/退出
  5. [updater] 将从 [main-new] 重命名为 [main]
  6. [updater] 成功后,运行 [main] 并自行关闭/退出

*注意: 您不必使用 CMD 重命名文件,使用:

My.Computer.FileSystem.RenameFile("C:\Test.txt", "SecondTest.txt")

*来自Rename File

希望对您有所帮助。

【讨论】:

  • 哦,哇,这很方便 - 但它有一种方法可以用一个 .exe 做到这一点,比如可能将 2 个 .exe 组合/存储/压缩成一个,然后加载另一个然后需要?跨度>
  • 您可能想使用 DLL 并根据需要加载卸载。我认为您需要将 .EXE 设计为启动器,并尽可能将您的应用程序划分为 DLL 模块,如果需要任何修订,您只需更新 DLL。
  • 好主意,但我认为对于我的轻量级应用程序来说,这样做的工作量太大了:/
  • 好吧,我从一个新的角度尝试这个——我在应用程序事件文件中,我将我的 UpdateChecker 代码添加为一个函数,其中一些略有改变——然后我在启动事件中检查函数是否显示有一个更新,并将新的 exe 下载到应用程序打开的任何位置。一切正常,但问题是如果有更新,我如何阻止应用程序加载?我试着用 Hide() 和 Close() 做 Form1_Load 但由于某种原因,即使我这样做了,我的应用程序仍然保持打开状态:/
  • 这似乎是由于我的关闭动画 - 正在处理它。
猜你喜欢
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
  • 2021-02-05
  • 1970-01-01
  • 2018-02-26
  • 2012-03-31
相关资源
最近更新 更多