Secondary Menu

Adding Code to a Visual C#.Net Assembly That Informs the User for an Update

Introduction: Adding Code to a Visual C#.Net Assembly That Informs the User for an Update

Not everybody is as technical as you are.

You build an application in c#, debug it, and give it to them.

Do you really think they're going to check your website every once in a blue moon and see if there is an update for your program? Nope. They are going to stay stuck on the version you gave them, and soon enough, well, when you release version 10.7.0.349 you'll find out most of your customers will be using version 1.0.0.000.

This instructable is devoted to fixing that.

What you need:

(At the least, basic) skills in Visual C#

A Computer running Windows 8 or later

Visual Studio 2017 installed

What we are aiming for:

Every [ <Specified amount of time> ] , we want the application to check a text file on your website with the latest version in it. If the latest version in the text file is larger than the current version, then the application should pop up a Dialog Box. Then Miss Piggy can decide whether to update her picture-editing app or not.

Step 1: Get Started First

Please open Visual Studio. (While you are waiting for it to start, grab some M&M's)

Good, now open the project you wanted to add update code to.

Step 2: Program.cs

Now go to Program.cs for your project. This can be accomplished by opening the Solution Explorer (by pressing "View" on the menu bar and clicking "Solution Explorer") and looking for Program.cs then double-clicking on it.

Step 3: The C# Code

Scroll down to just before the mysterious "Application.Run( new Form1());".

Now create a System.Windows.Forms.Timer.

Create a new event handler for Timer.Tick. Like this:

timer1.Tick += new EventHandler(timer1_Tick);

Now:

timer1.Interval = 604800000; // in milliseconds

timer1.Start();

Change the timer1.Interval to whatever you want. Be warned, it is in milliseconds.

This is how often the program checks for an update. (I would recommend around two weeks)

Tip

It is vital that this piece of code be put in Program.cs and before the "Application.Run".

Step 4: More Code

Create a new method for the Timer - Ticking. (This tell the computer that every time the computer ticks to do something)

This should be AFTER the Application.Run.

void timer1_Tick(object sender, EventArgs e) { UpdateNow(version); }

Now create the version string: This should be the current version of the Product.

Also, create a new Uri (URL).

This should be BEFORE the Application.Run.

string version = "5.5.0.000";

Uri x = new Uri(null); // For those who don't code, this is a URL

I know, the above code is going to throw an ArgumentNullException at me, but we'll change the null to something else soon enough.

Step 5: Adding More Code

Now we need to create the UpdateNow(string version) method.

The method should be UNDER the "Application.Run();".

void UpdateNow(string currentversion)

{

var http = new WebClient();

byte[] versionString = http.DownloadData(x);

string gfd = Encoding.UTF8.GetString(versionString);

Version latestVersion = new Version(gfd); //get my own version to compare against latest.

{ DialogResult dlgres = MessageBox.Show("An update is available for your app. Would you like to update?", "Your App needs an Update", MessageBoxButtons.YesNo);

if (dlgres == DialogResult.Yes)

{ Process.Start(uurl);

}

}

}

This code will be explained later on.

Step 6: Hosting Text Files

Now create a .txt file on your local Windows machine. Call it LatestVersion.txt.

Type the following values in:

1.0.0.000

Substitute "1.0.0.000" with the latest version of your product.

Step 7: Now Its Time . . . .

. . . . To actually host the files.

Get over to your website DNS provider (e.g. Wordpress, Zohosites, Wix, Weebly, GoDaddy) and upload the .txt file we just created. Then, using whatever method you prefer, get the URL of the text file.

Recall that in Step 4 we created a new URI and we left the parameter null. Of course, we can't leave it like that, so change it to the URL of the text file.

Also, before we go on any further, change the string "uurl" (See the UpdateNow() method) to the URL where the user would download the latest update to the application.

Step 8: What YOU Need to Do

Whenever you release the latest version, you must DELETE the text file from your website. Create a new text file with the same name, then upload it again AT THE SAME LOCATION ON YOUR WEBSITE.

Step 9: The Code Explained

Every time timer1 ticks (every 604800000 milliseconds, or every seven days) it runs the UpdateNow method and passes the version string as the parameter. The method initiates a new instance of a WebClient (System.Net.WebClient) and asks it to download the string from the text URL as a byte array; basically, it downloads a copy of the text files as a byte array. Then it converts it into a string, with which it creates a new instance of an AppVersion. It then compares the version from the text file to the current version. If the latestversion, the version from the text file, is larger than the currentversion, then it pops up a dialog box to inform the user that an update is available. If the user wishes to update, it starts the update link in the default browser. Then the user will download/update his application.

And don't forget to publish your thoughts on it in the comments below. Tell me whether it's a good idea!