.Net Core

How to check for application version updates in .Net Core

As a user of the various software, we often see that every program we use has an About page. The About page shows the different information such as the author’s name, email, website, build date of the program and the current version of the software. We as a developer may be curious about making an About page for the software we build for our clients, so the clients can see how if the developer has released a new version, or in the other way, you can tell your clients to update the software from About page. In this article, we will make an About page which can check from the server if a new version is available. Let’s see the demo below:

Fire up your Visual Studio and create a new “.Net Core WPF App” project. The application is divided into two parts. In the first part, we will design the layout of the app, and then we will write the backend. Let’s start designing the layout first.

The design layout will be divided into two parts, the top and the bottom. On the top, we will show the software name, version information, last build date and a button to check for the new version. On the bottom side, we will show the author’s name, email and website. We will design this layout using the Grid element in XAML. The following piece of code will create a layout, which will include one column and two rows (for the top and the bottom).

<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.72*"/>
<RowDefinition Height="0.28*"/>
</Grid.RowDefinitions>

After writing this code inside the Grid element, change the height of the window to 340 and width to 670. We will get the layout like the following screenshot:

Let’s design the top of the layout now. We will divide the top into two sides, the left side and the right side. On the left side, we will display all the software information and on the left side, we will show the logo. For this, we will define another grid inside out top row of the current layout, and that grid will have two columns (left and right) and one row. The following code will do that for us: write this code after </Grid.RowDefinitions> and before </Grid>.

<Grid x:Name="Top" Grid.Row="0" Background="#202340">
<!--Start of Grid 2-->
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="0.65*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.35*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid> <!--End of Grid 2-->

Notice the Background=”#202340″ at line 1. It is used to change the background of the grid. Our updated layout will look like the following:

Before proceeding further, let’s add a gif image to animate when the user clicks on check for update button, and add a style for that button in “App.xaml” file.
To show the gif images, we need to include a NuGet package called “WpfAnimatedGif” in our project. Go to tools -> NuGet Package Manager -> Package Manager Console, type “Install-Package WpfAnimatedGif” and press the enter key. Once it gets installed, add its reference as a namespace to the “XAML” file as follows:

xmlns:gif=”http://wpfanimatedgif.codeplex.com”

Now let’s add the style for the button. Put the following code in App.xaml file:

<Style x:Key="Button.Hoverless" TargetType="{x:Type ButtonBase}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border Name="border"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="Selector.IsSelected" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="#FFBEE6FD" />
</MultiTrigger>

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="Selector.IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="#BB90EE90" />
</MultiTrigger>

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="False" />
<Condition Property="Selector.IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="LightGreen" />
</MultiTrigger>

<Trigger Property="IsPressed" Value="True">
<Setter TargetName="border" Property="Opacity" Value="0.95" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="Opacity" Value="0.80" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Now, let’s come back for designing our layout. Let’s put our controls here on the left side of the top row. The following lines of code will create some controls, which includes the labels to show the software information a button to check for a new version: Place the code before </Grid> <!–End of Grid 2–>

<Grid Grid.Row="0" Grid.Column="0">
<!--Start of Grid 3-->
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.15*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.15*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1" Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="0.5*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.5*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label FontFamily="Calibri" FontWeight="Bold" Foreground="White" HorizontalAlignment="Right" VerticalAlignment="Center" FontSize="18" Grid.Row="0" Grid.Column="0" Content="My Amazing"/>
<Label FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="18" Grid.Row="0" Grid.Column="1" Content="Company"/>
</Grid>
<Label FontFamily="Calibri" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16" Grid.Row="2" Grid.Column="0" Content="Calibration & Testing Management System"/>
<Label Name="lblVersion" Margin="73,0,0,0" FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16" Grid.Row="3" Grid.Column="0" Content="Version: 1.2.0"/>
<Label Name="lblLastBuildDate" Margin="73,0,0,0" FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16" Grid.Row="4" Grid.Column="0" Content="Last build date: D M Y"/>
<Grid Grid.Row="5" Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="0.17*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.24*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.58*"/>
</Grid.ColumnDefinitions>
<Image Name="imgCircle" Visibility="Collapsed" Width="100" Height="100" gif:ImageBehavior.AnimatedSource="/circle.gif" />
<Button Name="btnCheckForUpdate" Click="btnCheckForUpdate_Click" Foreground="Firebrick" Background="#231c63" Grid.Column="1" Grid.Row="0" Content="Check for update" Width="100" Height="25"/>
<Label Visibility="Hidden" Name="lblUpdateOrError" FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16" Grid.Row="0" Grid.Column="2" Content="The software is up to date"/>
<StackPanel x:Name="stpNewVersionAvailable" Visibility="Collapsed" Orientation="Horizontal" Grid.Row="0" Grid.Column="2">
<Label FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16" Content="A"/>
<TextBlock FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center">
<Hyperlink Foreground="White" FontSize="14" RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="https://google.com">new version</Hyperlink>
</TextBlock>
<Label FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16" Content="is available"/>
</StackPanel>
</Grid>
</Grid>
<!--End of Grid 2-->

So far, our layout is looking like the following screenshot:

Let’s add a logo on the top-right side. For this, we just need to add an Image element on the second column of our first grid. Write the code below just after the </Grid> <!–End of Grid 2–>:

<strong><Image</strong> Source<strong>=</strong>"/fish.png" Grid.Column<strong>=</strong>"1" Grid.Row<strong>=</strong>"0"<strong>/></strong>

Our application will look like the following screenshot:

We just need to fill up the bottom row of our layout. We will put some elements which will show the Author information along with some icons. Put the below code just after </Grid><!–End of Grid 3–>

<Grid x:Name="Bottom" Grid.Row="1" Background="#171932">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.40*"/>
<RowDefinition Height="0.60*"/>
<RowDefinition Height="0.20*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Label FontFamily="Calibri" Foreground="White" FontSize="14" Grid.Row="0" Grid.Column="0" Content="Developed by:"/>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="0.17*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.22*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.31*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.20*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label FontFamily="Calibri" Margin="0,7,0,0" Foreground="White" FontSize="14" Grid.Row="0" Grid.Column="0" Content="Kamal Ashraf Gill"/>
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1">
<Image Name="loadingGif" Margin="0,-10,0,0" Visibility="Collapsed" Width="50" Height="50" gif:ImageBehavior.AnimatedSource="/circle.gif"/>
<Label FontFamily="Calibri" Margin="0,8,0,0" Foreground="White" FontSize="14" Content="+92-300-000-0000"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="2" Grid.Row="1">
<Image Source="/EmailIcon.png"/>
<Label FontFamily="Calibri" Margin="0,8,0,0" Foreground="White" FontSize="14" Content="[email protected]"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="3" Grid.Row="1">
<Image Source="WebIcon.png" Width="28"/>
<TextBlock Margin="8,12,0,0" Foreground="White">
<Hyperlink FontFamily="Calibri" Foreground="White" FontSize="14" RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="http://www.kamalashraf.com">kamalashraf.com</Hyperlink>
</TextBlock>
</StackPanel>
</Grid>
</Grid>

Now, just change the title of the App to “About” and set ResizeMode=”NoResize” inside the “Window” element, so the app will only have a close button in the title bar. Our final layout design will look like the following screenshot:

The entire code for the XAML file is as follows:

<Window x:Class="Check_New_Version.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:gif="http://wpfanimatedgif.codeplex.com"
xmlns:local="clr-namespace:Check_New_Version"
mc:Ignorable="d"
Title="About" ResizeMode="NoResize" Height="340" Width="670">
<Grid> 
<!--Start of Grid 1-->
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.72*"/>
<RowDefinition Height="0.28*"/>
</Grid.RowDefinitions>
<Grid x:Name="Top" Grid.Row="0" Background="#202340">
<!--Start of Grid 2-->
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="0.65*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.35*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Grid.Column="0">
<!--Start of Grid 3-->
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.15*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.15*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1" Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="0.5*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.5*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label FontFamily="Calibri" FontWeight="Bold" Foreground="White" HorizontalAlignment="Right" VerticalAlignment="Center" FontSize="18" Grid.Row="0" Grid.Column="0" Content="My Amazing"/>
<Label FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="18" Grid.Row="0" Grid.Column="1" Content="Company"/>
</Grid>
<Label FontFamily="Calibri" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16" Grid.Row="2" Grid.Column="0" Content="The Amazing App"/>
<Label Name="lblVersion" Margin="73,0,0,0" FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16" Grid.Row="3" Grid.Column="0" Content="Version: 1.2.0"/>
<Label Name="lblLastBuildDate" Margin="73,0,0,0" FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16" Grid.Row="4" Grid.Column="0" Content="Last build date: D M Y"/>
<Grid Grid.Row="5" Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="0.17*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.24*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.58*"/>
</Grid.ColumnDefinitions>
<Image Name="loadingGif" Margin="0,-10,0,0" Visibility="Collapsed" Width="50" Height="50" gif:ImageBehavior.AnimatedSource="/circle.gif"/>
<Button Name="btnCheckForUpdate" Click="btnCheckForUpdate_Click" Foreground="Firebrick" Background="#231c63" Grid.Column="1" Grid.Row="0" Content="Check for update" Width="100" Height="25"/>
<Label Visibility="Hidden" Name="lblUpdateOrError" FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16" Grid.Row="0" Grid.Column="2" Content="The software is up to date"/>
<StackPanel x:Name="stpNewVersionAvailable" Visibility="Collapsed" Orientation="Horizontal" Grid.Row="0" Grid.Column="2">
<Label FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16" Content="A"/>
<TextBlock FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center">
<Hyperlink Foreground="White" FontSize="14" RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="https://google.com">new version</Hyperlink>
</TextBlock>
<Label FontFamily="Calibri" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16" Content="is available"/>
</StackPanel>
</Grid>
</Grid>
<Image Source="/fish.png" Grid.Column="1" Grid.Row="0"/>
<!--End of Grid 2-->

</Grid>
<!--End of Grid 3-->
<Grid x:Name="Bottom" Grid.Row="1" Background="#171932">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.40*"/>
<RowDefinition Height="0.60*"/>
<RowDefinition Height="0.20*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Label FontFamily="Calibri" Foreground="White" FontSize="14" Grid.Row="0" Grid.Column="0" Content="Developed by:"/>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" Width="0.17*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.22*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.31*"/>
<ColumnDefinition SharedSizeGroup="A" Width="0.20*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label FontFamily="Calibri" Margin="0,7,0,0" Foreground="White" FontSize="14" Grid.Row="0" Grid.Column="0" Content="Kamal Ashraf Gill"/>
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1">
<Image Source="/PhoneIcon.png" Width="28"/>
<Label FontFamily="Calibri" Margin="0,8,0,0" Foreground="White" FontSize="14" Content="+92-300-000-0000"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="2" Grid.Row="1">
<Image Source="/EmailIcon.png"/>
<Label FontFamily="Calibri" Margin="0,8,0,0" Foreground="White" FontSize="14" Content="[email protected]"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="3" Grid.Row="1">
<Image Source="WebIcon.png" Width="28"/>
<TextBlock Margin="8,12,0,0" Foreground="White">
<Hyperlink FontFamily="Calibri" Foreground="White" FontSize="14" RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="http://www.kamalashraf.com">kamalashraf.com</Hyperlink>
</TextBlock>
</StackPanel>
</Grid>
</Grid>
</Grid> <!--End of Grid 1-->
</Window>

Let’s move to the backend part.

As the app will show an animated image while checking for the new version in the background, so it means that the app will be performing more than one task in the background. It means there should be another thread other than the main thread which will be checking for the new version. To perform multi-threading in our application, we have to use The Background worker here. When we will click on the button for checking the new version, the background worker will check for it in the background while keeping the main thread responsive.

The Background worker has three main methods, meaning it can be split into three parts. The three main methods are, DoWork and RunWorkerComplete. The DoWork method can be bonded to a method which will be responsible for performing the job, and the RunWorkerComplete will be bounded to a method which will be executed after the job is done.

Let’s declare the Background worker inside public partial class AboutPage : Window: Remember to add using System.ComponentModel; on the top of the file. We will also add a string variable containing the current version of our app, and three other variables which will contain the information if the software is already updated, is there a new version available and if we got any problem while checking for the new version:

BackgroundWorker worker = new BackgroundWorker();
string currentVersion = "1.2.0";
bool alreadyUpated = false;
bool upatedAvailable = false;
bool problemChecking = false;

We will start checking for a new version when the user presses the button. So, we have to write the code inside the event listener of the button. In the button’s event listener, before starting the background worker, we will hide all the labels if they are already shown on the screen, and will set the visibility of the gif animated image to “visible”. The following code will do that:

lblUpdateOrError.Visibility = Visibility.Collapsed;
stpNewVersionAvailable.Visibility = Visibility.Collapsed;
btnCheckForUpdate.IsEnabled = false;
loadingGif.Visibility = Visibility.Visible;

Now, write a method which will check for the new version:

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    try
    {
        Thread.Sleep(2000); //Add some delay to see the animation
        WebClient client = new WebClient();
        Stream stream = client.OpenRead("http://kamalashraf.com/version.txt");
        StreamReader reader = new StreamReader(stream);
        String newVersion = reader.ReadToEnd();
        if (newVersion.CompareTo(currentVersion) == 1)
        {
            updateAvailable = true; 
        }
        else
        {
            alreadyUpated = true;
        }
    }
    catch (WebException)
    {
        worker.CancelAsync();
        if (worker.CancellationPending)
        {
            e.Cancel = true;
        }
        problemChecking = true;
    }
}

Now, add a new method which will run after the background worker will do checking the new version:

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    loadingGif.Visibility = Visibility.Collapsed;
    btnCheckForUpdate.IsEnabled = true;
    if (updateAvailable)
    {
        stpNewVersionAvailable.Visibility = Visibility.Visible;
    }
    else if (alreadyUpated)
    {
        lblUpdateOrError.Visibility = Visibility.Visible;
    }
    else if (problemChecking)
    {
        lblUpdateOrError.Visibility = Visibility.Visible;
        lblUpdateOrError.Content = "A network error occurred.";
    }
}

Let’s bind both methods to the background worker’s DoWork and RunWorkerComplete method respectively. Add the following lines at the bottom of the button’s event listener:

worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.RunWorkerAsync();

Now, add the following line of code to the “Hyperlink_RequestNavigate” method. This method is responsible to open the link for the new version.

Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) {
    UseShellExecute = true }
);

The complete code for the MainWindow.xaml.cs can be found below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Check_New_Version
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        BackgroundWorker worker = new BackgroundWorker();
        string currentVersion = "1.2.0";
        bool alreadyUpated = false;
        bool updateAvailable = false;
        bool problemChecking = false;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri)
            {
                UseShellExecute = true
            }
            );
        }

        private void btnCheckForUpdate_Click(object sender, RoutedEventArgs e)
        {
            lblUpdateOrError.Visibility = Visibility.Collapsed;
            stpNewVersionAvailable.Visibility = Visibility.Collapsed;
            btnCheckForUpdate.IsEnabled = false;
            loadingGif.Visibility = Visibility.Visible;
            worker.DoWork += worker_DoWork;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Thread.Sleep(2000); //Add some delay to see the animation
                WebClient client = new WebClient();
                Stream stream = client.OpenRead("http://kamalashraf.com/version.txt");
                StreamReader reader = new StreamReader(stream);
                String newVersion = reader.ReadToEnd();
                if (newVersion.CompareTo(currentVersion) == 1)
                {
                    updateAvailable = true;
                }
                else
                {
                    alreadyUpated = true;
                }
            }
            catch (WebException)
            {
                worker.CancelAsync();
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                }
                problemChecking = true;
            }
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            loadingGif.Visibility = Visibility.Collapsed;
            btnCheckForUpdate.IsEnabled = true;
            if (updateAvailable)
            {
                stpNewVersionAvailable.Visibility = Visibility.Visible;
            }

            else if (alreadyUpated)
            {
                lblUpdateOrError.Visibility = Visibility.Visible;
            }

            else if (problemChecking)
            {
                lblUpdateOrError.Visibility = Visibility.Visible;
                lblUpdateOrError.Content = "A network error occurred.";
            }
        }
    }
}

Just run the application and you’ll see that there is a new version available. The link for the new version is set to google.com, but you can change this to the link of your application. You can change the value of “currentVersion” and change it to as same as the new version from the server. (The new version contains the version 1.2.5). The source code for the whole project can be downloaded here

Write A Comment