[C#] How to ask user to update the application automatically

binh12A3
5 min readJul 22, 2023

By using the offical “Microsoft Visual Studio Installer Projects” to produce a “msi” file and “dropbox” as a webserver.

1. Create a simple demo app

2. Install “Microsoft Visual Studio Installer Projects”

3. Add the necessary references

4. Prepare and upload files on dropbox

We need to create an “Update.txt” file which contains the version, and a dummy “MyAppSetup.zip”

5. Code

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.IO;
using System.IO.Compression;
using System.Net;
using System.Diagnostics;
namespace MyApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

const string VERSION = "1.0.0";

// ** NOTE ** replace at the of the link "dl=0" with "dl=1" to be auto-download
// link to Update.txt
const string TXT_LINK = "https://www.dropbox.com/scl/fi/cc7f9pbbcfqbrf1myvb6h/Update.txt?rlkey=kklp6ia11g3xync1hyvr14n4t&dl=1";
// link to MyAppSetup.zip
const string ZIP_LINK = "https://www.dropbox.com/scl/fi/uidhpgb21l7dqdhk0vvoz/MyAppSetup.zip?rlkey=oxsrussqa9tmuajgeic3sy729&dl=1";

WebClient webClient = new WebClient();
var client = new WebClient();

if (!webClient.DownloadString(TXT_LINK).Contains(VERSION))
{
if (MessageBox.Show("New update available ! Do you want to install it ?", "DemoApp", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
if (File.Exists(@".\MyAppSetup.msi"))
{
File.Delete(@".\MyAppSetup.msi");
}
client.DownloadFile(ZIP_LINK, @"MyAppSetup.zip");
string zipPath = @".\MyAppSetup.zip";
string extractPath = @".\";
ZipFile.ExtractToDirectory(zipPath, extractPath);
Process process = new Process();
process.StartInfo.FileName = "msiexec";
process.StartInfo.Arguments = String.Format("/i MyAppSetup.msi");
this.Close();
process.Start();
}
catch
{
}
}
}
}
}
}

6. Create Setup project

Add “Project Output…”

Rename to “MyApp” then drag and drop into the folder “User’s Desktop”

Build “MyApp” and “MyAppSetup”

Open folder in File Explorer then run and install “MyAppSetup”

6. Change and update new version

  • Make a simple GUI change
  • Update version from “1.0.0” to “2.0.0”
  • Build “MyApp” and “MyAppSetup”
  • Create a new “MyAppSetup.zip”
  • Upload the new 2 files to dropbox (they’ll replace the all files)
  • Then when user open the app, they’ll see the pop up window, and they can click “Yes” to download and install the new version.

— — — — — — — — — — — -— - — THE END — — — — — — — — — -— — — -—

--

--