VB.NET,C#

[vb.net] 간단한 자동 업데이트 프로그램 만들기

지니허니 2024. 2. 5. 15:08

 

VB.NET에서 프로그램 버전 체크와 자동 업데이트를 구현하려면 서버에서 제공되는 버전 정보를 확인하고, 필요한 경우 업데이트 파일을 다운로드하여 설치하는 방법을 사용합니다.
자동 업데이트 기능을 구현하려면, VB.NET에서는 일반적으로 System.Net.WebClient 클래스나 System.IO와 같은 네트워크 및 파일 처리 클래스를 사용할 수 있습니다. 
아래는 간단한 예제 코드로서, My.Settings를 이용하여 현재 버전과 서버의 최신 버전을 비교하고 업데이트를 진행하는 소스 코드입니다.

 

Imports System.Net
Imports System.IO

Public Class form1

    Private Const serverVersionUrl As String = "http://yourupdateurl.com/version.txt" ' 서버에서 제공하는 최신 버전 정보 파일 URL
    Private serverVersion As String

    Public Sub CheckForUpdate()
        Try
            ' 서버에서 최신 버전 정보를 가져옴
            Dim webClient As New WebClient()
            serverVersion = webClient.DownloadString(serverVersionUrl)

            ' 현재 버전과 서버의 최신 버전을 비교
            If CompareVersions(My.Application.Info.Version.ToString(), serverVersion) < 0 Then
                ' 업데이트가 필요한 경우
                MessageBox.Show("새로운 버전이 있습니다. 업데이트를 시작합니다.", "업데이트 알림", MessageBoxButtons.OK, MessageBoxIcon.Information)
                DownloadAndUpdate()
            Else
                ' 최신 버전을 사용 중인 경우
                MessageBox.Show("현재 사용 중인 버전이 최신입니다.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            ' 버전 체크 또는 다운로드 중 오류 발생 시 처리
            MessageBox.Show("업데이트 체크 중 오류가 발생했습니다." & vbCrLf & ex.Message, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DownloadAndUpdate()
        Try
            ' 업데이트 파일을 다운로드할 경로 및 파일명 설정
            Dim updateUrl As String = "http://yourupdateurl.com/updatefile.zip"
            Dim localFilePath As String = "C:\updatefile.exe"

            ' 웹 클라이언트를 생성하여 업데이트 파일 다운로드
            Dim webClient As New WebClient()
            webClient.DownloadFile(updateUrl, localFilePath)

            ' 여기에서 업데이트 파일을 압축 해제하거나 필요한 작업을 수행할 수 있습니다.

            ' 업데이트 완료 후 실행되는 코드
            MessageBox.Show("업데이트가 완료되었습니다.", "업데이트 완료", MessageBoxButtons.OK, MessageBoxIcon.Information)

            ' 업데이트 파일 실행 (예: 프로그램 다시 시작)
            Process.Start(localFilePath)

            ' 현재 프로그램 종료
            Application.Exit()
        Catch ex As Exception
            ' 업데이트 중 오류 발생 시 처리
            MessageBox.Show("업데이트 중 오류가 발생했습니다." & vbCrLf & ex.Message, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Function CompareVersions(currentVersion As String, latestVersion As String) As Integer
        ' 버전 비교를 위한 간단한 구현
        Dim currentParts As String() = currentVersion.Split("."c)
        Dim latestParts As String() = latestVersion.Split("."c)

        For i As Integer = 0 To Math.Min(currentParts.Length, latestParts.Length) - 1
            Dim currentPart As Integer = Integer.Parse(currentParts(i))
            Dim latestPart As Integer = Integer.Parse(latestParts(i))

            If currentPart < latestPart Then
                Return -1 ' 현재 버전이 최신 버전보다 낮음
            ElseIf currentPart > latestPart Then
                Return 1 ' 현재 버전이 최신 버전보다 높음
            End If
        Next

        Return 0 ' 버전이 같음
    End Function
    
    ' 사용법 폼로드에 CheckForUpdate() 넣어주면 됩니다.    
    Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   
   '인터넷 연결 되어 있는 확인 코드
    If My.Computer.Network.IsAvailable Then 
    '인터넷 연결이 되어있으면 업데이트 체크
    CheckForUpdate()
Else '아니면
    MsgBox("인터넷에 연결이 되어 있지 않습니다.")
End If
        
    End Sub
End Class

 

 

이 코드에서 serverVersionUrl은 서버에서 제공되는 최신 버전 정보를 담고 있는 텍스트 파일의 URL입니다. 이 파일은 단순히 최신 버전을 나타내는 문자열을 포함하고 있어야 합니다.


버전 비교는 간단한 방법으로 이루어졌으며, 실제 프로젝트에 맞게 더 강력한 버전 비교 로직을 구현할 수 있습니다. 또한, 업데이트 파일 다운로드 후의 작업은 프로젝트의 특성에 따라 다르게 구현될 수 있습니다.

 

복사가 안되면 아래 첨부 파일을 다운받아 복사하세요.

 

[vb.net] 간단한 자동 업데이트 프로그램 만들기.txt
0.00MB

 

이상으로 vb.net 으로 간단하게 자동업데이트 구현하는 방법에 대해 알아봤습니다.

도움이 되었길 바랍니다.