What's new

[C# TUT] Check for Update button.

  • Thread starter Fierce Waffle
  • Start date
  • Views 5,971
Fierce Waffle

Fierce Waffle

Enthusiast
Messages
472
Reaction score
129
Points
155
Sin$
0
In this tutorial I will be showing you how to make your programming check for updates by checking an xml website. I recommend 110mb.com for free hosting.


First of all make sure you have
Code:
using System.Xml
at the top.

Now create a new button and double click it to make a new event handler. This is the button we will be using to check for updates.
Code:
private void updButton_Click_1(object sender, EventArgs e)
{
string downloadUrl = "";
Version newVersion = null;
string aboutUpdate = "";
string xmlUrl = "http://www.fiercemodder.com/Fierce%20Modder/update.xml";
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(xmlUrl);
reader.MoveToContent();
string elementName = "";
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "appinfo"))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
elementName = reader.Name;
}
else
{
if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
switch (elementName)
{
case "version":
newVersion = new Version(reader.Value);
break;
case "url":
downloadUrl = reader.Value;
break;
case "about":
aboutUpdate = reader.Value;
break;
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Environment.Exit(1);
}
finally
{
if (reader != null)
reader.Close();
}
Version applicationVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
if (applicationVersion.CompareTo(newVersion) < 0)
{
string str = String.Format("New version found!\nYour version: {0}.\nNewest version: {1}. \nAdded in this version: {2}. ", applicationVersion, newVersion, aboutUpdate);
if (DialogResult.No != MessageBoxEx.Show(str + "\nWould you like to download this update?", "Check for updates", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
try
{
Process.Start(downloadUrl);
}
catch { }
return;
}
else
{
;
}
}
else
{
MessageBoxEx.Show("Your version: " + applicationVersion + "  is up to date.", "Check for Updates", MessageBoxButtons.OK, MessageBoxIcon.None);
}
}

Now what that code is doing is its checking an XML file on your file server.

Here is what the Xml file should be formatted like
Code:
<?xml version="1.0" encoding="utf-8" ?>
<appinfo>
<version>1.5.0.0</version>
<url>http://www.fiercemodder.com/Fierce%20Modder/v1.5.rar</url>
<about>Remapped UI. Get License file from me</about>
</appinfo>

the about section of the XML tells the user about whats in the new update. version shows the new version. The code compares that versus the assembly version of you application. Feel free to ask any other Q's.
 
trippinz

trippinz

Enthusiast
Programmer Grammar Nazi Frame In Gold
Messages
839
Reaction score
200
Points
175
Sin$
0
WOW!

my way:

Code:
private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{

WebClient wc = new WebClient();
try
{
string UI = wc.DownloadString("http://www.otrippinz.com/XLBC_UpdateChecker.TXT");

if (UI.Contains("V4.0.0"))
{
MessageBoxEx.Show("Xbox Live Bio Art Creator is running on the latest version.", "Up to Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (MessageBoxEx.Show("An update is available, would you like to update to version " + UI + "?", "New Version Available!", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
Process.Start(wc.DownloadString("http://www.otrippinz.com/XLBC_UpdateLink.TXT"));
}
}

}

catch
{
MessageBoxEx.Show("For some reason the program cannot connect to my site, this may be due to a couple of things:\n\n-Server Maintenance in which case you should try checking for updates in a couple of hours\n-The second reason may be unfortunately I may have stopped webhosting or moved to a new server, in which case email me at any of the following:\n\[email protected]\[email protected]\[email protected]\n\n-You may not be connected to the internet...", "Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
 
DarkAmbur

DarkAmbur

S7 Awesomeness
Experienced Veteran Tutorial Creator Mr. Nice Guy
Messages
1,765
Reaction score
443
Points
305
Sin$
7
WOW!

my way:

Code:
private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{

WebClient wc = new WebClient();
try
{
string UI = wc.DownloadString("http://www.otrippinz.com/XLBC_UpdateChecker.TXT");

if (UI.Contains("V4.0.0"))
{
MessageBoxEx.Show("Xbox Live Bio Art Creator is running on the latest version.", "Up to Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (MessageBoxEx.Show("An update is available, would you like to update to version " + UI + "?", "New Version Available!", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
Process.Start(wc.DownloadString("http://www.otrippinz.com/XLBC_UpdateLink.TXT"));
}
}

}

catch
{
MessageBoxEx.Show("For some reason the program cannot connect to my site, this may be due to a couple of things:\n\n-Server Maintenance in which case you should try checking for updates in a couple of hours\n-The second reason may be unfortunately I may have stopped webhosting or moved to a new server, in which case email me at any of the following:\n\[email protected]\[email protected]\[email protected]\n\n-You may not be connected to the internet...", "Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Lol, his checks the current assembly version and tells you whats in the new version and is much more organized.
 
trippinz

trippinz

Enthusiast
Programmer Grammar Nazi Frame In Gold
Messages
839
Reaction score
200
Points
175
Sin$
0
Lol, his checks the current assembly version and tells you whats in the new version and is much more organized.

You could easily implement that into my code. I just find it easier this way as sometimes if you mess up on the assembly name you still have a net version that can trigger the update
 
Triiistan

Triiistan

Contributor
Experienced Veteran Programmer Grammar Nazi
Messages
1,951
Reaction score
494
Points
270
Sin$
0
WOW!

my way:

Code:
private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{
 
WebClient wc = new WebClient();
try
{
string UI = wc.DownloadString("http://www.otrippinz.com/XLBC_UpdateChecker.TXT");
 
if (UI.Contains("V4.0.0"))
{
MessageBoxEx.Show("Xbox Live Bio Art Creator is running on the latest version.", "Up to Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (MessageBoxEx.Show("An update is available, would you like to update to version " + UI + "?", "New Version Available!", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
Process.Start(wc.DownloadString("http://www.otrippinz.com/XLBC_UpdateLink.TXT"));
}
}
 
}
 
catch
{
MessageBoxEx.Show("For some reason the program cannot connect to my site, this may be due to a couple of things:\n\n-Server Maintenance in which case you should try checking for updates in a couple of hours\n-The second reason may be unfortunately I may have stopped webhosting or moved to a new server, in which case email me at any of the following:\n\[email protected]\[email protected]\[email protected]\n\n-You may not be connected to the internet...", "Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I am using your code in one of my programs, and need a little bit of help. I got everything working, except the updatelink.txt file is not working. Every time I click "Yes" to update, I get that error about internet being down, etc. This is the link to my updatelink.txt file: http://itristan.webs.com/updatelink.txt Can you help me out?
 
Xeren

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
Triiistan

Triiistan

Contributor
Experienced Veteran Programmer Grammar Nazi
Messages
1,951
Reaction score
494
Points
270
Sin$
0
I don't quite understand Xml or a lot that is in the tutorial. The other example is good for me, I am just messed up in one part.

Code:
if (MessageBox.Show("An update is available, would you like to update to version " + UI + "?", "New Version Available!", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        wc.DownloadString("http://www.mediafire.com/?o8ldai0168072w9");
                    }
The download string is not working, how can I have C# visit a web link when the user clicks yes to update?
 
Xeren

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
I don't quite understand Xml or a lot that is in the tutorial. The other example is good for me, I am just messed up in one part.

Code:
if (MessageBox.Show("An update is available, would you like to update to version " + UI + "?", "New Version Available!", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
wc.DownloadString("[URL]http://www.mediafire.com/?o8ldai0168072w9[/URL]");
}
The download string is not working, how can I have C# visit a web link when the user clicks yes to update?
Lol, really? It is simply:
Code:
System.Diagnostics.Process.Start(link);
 
Top Bottom
Login
Register