You can do the following, based upon the format you expect, i.e. a version number consisting of integer parts separated by a dot ('.'):
var version = webBrowser.Document.Body.InnerText
.Split('.') // this splits the string into number parts
.Select(p => Int32.Parse(p)) // this converts each number part to an int
.ToArray() // this returns them as an array of integer values.
// This prints the version components
if (version.Length == 1)
Console.WriteLine("Version: {0}", version[0]);
else if (version.Length == 2)
Console.WriteLine("Version: Major = {0}, Minor = {1}", version[0], version[1]);
else if (version.Length == 3)
Console.WriteLine("Version: Major = {0}, Minor = {1}, Increment = {2}", version[0], version[1], version[2]);
else if (version.Length == 3)
Console.WriteLine("Version: Major = {0}, Minor = {1}, Increment = {2}, BuildNumber = {3}", version[0], version[1], version[2], version[4]);
else
Console.WriteLine("Version contains more than 4 ({0}) parts", version.Length);
Note, that this is not a single integer that you will get returned. You have to access the components individually if you want to do a comparison, I.e. compare version[0]
with the major
version that you are expecting, version[1]
with the minor
version component, etc.
A standard class that is already available specifically for version numbers formatted in this way is the Version class. You can create one from your text by doing this:
var version = new Version(webBrowser.Document.Body.InnerText);
// or alternatively:
//var version = Version.Parse(webBrowser.Document.Body.InnerText);
Console.WriteLine(version);
If you want to compare this with another version (let's say "2.3.1"), you
can do that like this:
var otherVersion = new Version("2.3.1");
// Or alternatively:
// var otherVersion = new Version(2, 3, 1);
// this compares two versions
var compareResult = version.CompareTo(otherVersion);
if (compareResult < 0)
Console.WriteLine("version < otherVersion");
else if (compareResult > 0)
Console.WriteLine("version > otherVersion");
else
Console.WriteLine("version == otherVersion");
When applied to the code snippet in your question, you can use it like this:
var currentVersion = new Version("0.0.1");
var version = Version.Parse(webBrowser.Document.Body.InnerText);
if (version.CompareTo(currentVersion) > 0)
{
// version is newer (higher) than our current version of "0.0.1"
}