MrLucky Posted April 10, 2006 Posted April 10, 2006 (edited) I'm trying to create a version checker in C#. But I have a little problem. I have a webpage, containing only the version. (http://ircbot.aoe3capitol.nl/e107_plugins/ircbot_menu/version.php) I want that version to a double in C# so I can compare it with the version of the program. But the problem is, when I use Convert.ToDouble or double.Parse the dots disapear, so the version will become 35 instead of 0.3.5. How can I fix this? My Function: private void Checkversion() { System.Net.WebClient wClient = new System.Net.WebClient(); byte[] buffer = wClient.DownloadData(@"http://ircbot.aoe3capitol.nl/e107_plugins/ircbot_menu/version.php"); double latest_version = double.Parse(System.Text.Encoding.Default.GetString(buffer, 0, buffer.Length)); double current_version = double.Parse(settings.Version); wClient.Dispose(); MessageBox.Show("Latest: " + latest_version.ToString() + "\nCurrent: " + current_version.ToString()); if(current_version < latest_version) { DialogResult result = MessageBox.Show("Er is een nieuwe versie van Lucky Bot beschikbaar. Wil je deze nu downloaden?", "Nieuwe versie", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if(result == DialogResult.Yes) { System.Diagnostics.Process.Start("http://sourceforge.net/project/showfiles.php?group_id=164639&package_id=186478"); Application.Exit(); } } } Edited April 10, 2006 by MrLucky Quote
Cags Posted April 10, 2006 Posted April 10, 2006 0.3.5 isn't a valid number as a number can only have one decimal place. A version number should be either treated as a string. Or treated as seperate Major and Minor numbers (plus any addition divisions required). So for your example of 0.3.5 you could store it as a string, then use Split to seperate it at the dots. You could then parse each individual part as a number to compare the version. Quote Anybody looking for a graduate programmer (Midlands, England)?
Cags Posted April 10, 2006 Posted April 10, 2006 That would certainly work for a simple comparison. I was just assuming that the application would perhaps act differently depending on if it is a major or minor version change. Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders snarfblam Posted April 10, 2006 Leaders Posted April 10, 2006 You might want to take a quick look at the System.Version class. After you convert the data to a string, you can create a Version object to easily compare parts of the version number. Quote [sIGPIC]e[/sIGPIC]
Diesel Posted April 11, 2006 Posted April 11, 2006 Ah....net 2005 makes me want to **** my pants...from excitement Quote
Cags Posted April 11, 2006 Posted April 11, 2006 It's worth noting this Class isn't specific to .Net 2.0 and was included in .Net 1.1. I'd personally never heard of it, but I'll keep it in mind incase I ever have to do something like this. One thing I would say is that (certainly in 1.1) there appears to be no way to parse the information from a string. Whilst it will make comparing version numbers easier it seems to me like you will still have to manually parse the data read in. (Obviously this doesn't apply if your comparing with a server that can return an object). Quote Anybody looking for a graduate programmer (Midlands, England)?
mskeel Posted April 11, 2006 Posted April 11, 2006 There is a constructor that takes a string as an argument. Quote
Cags Posted April 11, 2006 Posted April 11, 2006 My bad, your right. Quote Anybody looking for a graduate programmer (Midlands, England)?
mskeel Posted April 11, 2006 Posted April 11, 2006 It's cool. I only knew about it becuase I used it once. Otherwise, the version class is one of those obscure, single purpose classes that is really handy when you need it if you know about it and makes life difficult if you don't know about it. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.