Dưới đây là tóm tắt nhanh chức năng của mã này
- Kiểm tra phiên bản hiện tại với phiên bản mới nhất được lưu trữ trên hosting or github (có thể là bất kỳ trình xem văn bản thô nào)
- Nếu chúng không khớp, nó sẽ mở trình cập nhật rồi đóng chương trình hiện tại (chương trình lỗi thời)
- Trình cập nhật sẽ xóa phiên bản cũ rồi tải xuống phiên bản mới.
- Sau khi tải xuống, nó sẽ đóng và mở phiên bản cập nhật của công cụ gốc.
FULL CODE APPLICATION :
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespace Updater { class Program { // Credits to LTPF static void Main(string[] args) { WebClient wc = new WebClient(); if (File.Exists("AutoUpdater.exe")) // Nên là phiên bản lỗi thời.. File.Delete("AutoUpdater.exe"); File.WriteAllBytes("AutoUpdater.exe", wc.DownloadData("http://domain.xxx/AutoUpdater.exe")); // Tạo và ghi một tập tin khi nó đọc từ phiên bản mới nhất. Process.Start("AutoUpdater.exe"); // Khởi chạy chương trình gốc. Console.WriteLine("Update downloaded!\n"); } } }
Đối với URL domain.xxx mà tôi đã sử dụng máy chủ của mình, tôi không biết bất kỳ nơi lưu trữ tệp trực tuyến nào mà bạn chỉ cần nhập URL vào và nó sẽ tải xuống ngay lập tức.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.IO; using System.Diagnostics; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string currentVersion = "1.0.0.0"; private void Form1_Load(object sender, EventArgs e) { label1.Text = "Version: " + currentVersion; } private void button1_Click(object sender, EventArgs e) { WebClient wc = new WebClient(); string newVersion = wc.DownloadString("https:\\(trình xem văn bản thô chỉ chứa phiên bản, e.g. 1.0.0.0)"); // Tải xuống phiên bản mới nhất. if (currentVersion != newVersion) { if (!File.Exists("Updater.exe")) // Kiểm tra xem bản cập nhật tập tin không tồn tại hoặc đã được đổi tên. MessageBox.Show("Không tìm thấy trình cập nhật! Vui lòng tải lại chương trình!"); else { Process.Start("Updater.exe"); // Khởi chạy trình cập nhật nếu tệp KHÔNG tồn tại. Close(); } } else { MessageBox.Show("Cập nhật!"); } } } }
Và bên dưới là đoạn mã mà mình chuyển sang ngôn ngữ lập trình .NET
Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Linq Imports System.Text Imports System.Threading.Tasks Imports System.Windows.Forms Imports System.Net Imports System.IO Imports System.Diagnostics Namespace WindowsFormsApp1 Public Partial Class Form1 Inherits Form Public Sub New() InitializeComponent() End Sub Private currentVersion As String = "1.0.0.0" Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) label1.Text = "Version: " & currentVersion End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim wc As WebClient = New WebClient() Dim newVersion As String = wc.DownloadString("https:\(trình xem văn bản thô chỉ chứa phiên bản, e.g. 1.0.0.0)") If currentVersion <> newVersion Then If Not File.Exists("Updater.exe") Then MessageBox.Show("Không tìm thấy trình cập nhật! Vui lòng tải lại chương trình!") Else Process.Start("Updater.exe") Close() End If Else MessageBox.Show("Cập nhật!") End If End Sub End Class End Namespace
Chúc các bạn thành công với thủ thuật hay này nhé. Để tạo riêng cho mình một chương trình và có thông báo cập nhật phiên bản mới nếu có thay đổi gì từ hệ thống nhé.