Joseph
Joseph

Reputation: 21

C# How to check if number is larger?

I'm working on a program for my class at school and I'm setting up AutoUpdates. Is there any way to make "else if (webClient.DownloadString("mylink").Contains("0.3.9"))" check the contains of the link to see if it is over or greater than 0.3.9??

    public Form1()
    {
        InitializeComponent();
        WebClient webClient = new WebClient();  
        if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\updates\\Update-0.4.0.xml")) { }

        else if (webClient.DownloadString("mylink").Contains("0.3.9"))
        {
            if (MessageBox.Show("An Update is Avaliable, Would you like to download it?", "DesktopReborn Updater", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
            {
                webClient.DownloadFile("myupdate", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\DesktopReborn.exe");

                if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\updates\\Update-0.3.9.xml"))
                {
                    File.Copy(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\updates\\Update-0.3.9.xml", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\updates\\Update-0.4.0.xml", true);
                    File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\updates\\Update-0.3.9.xml");
                }
            }
        }
    }

Upvotes: 0

Views: 592

Answers (3)

TheGeneral
TheGeneral

Reputation: 81503

If your format is xx.xx.xx you could just parse it into a Version Class

private static Version _someAribtaryVersion = new Version("2.3.5")

...

var someFunkyNumber = webClient.DownloadString("mylink");

var version = new Version(someFunkyNumber);

if(version > _someAribtaryVersion)
   // Bingo!

Additional Resources

Version Class

Represents the version number of an assembly, operating system, or the common language runtime.

Also the advantage is it already comes with built in operators

Version.GreaterThan(Version, Version) Operator

Determines whether the first specified Version object is greater than the second specified Version object.

Which means you can compare 2 Versions with >, =, <, >=, <=

also some useful parsing methods

Version.Parse Method

Converts the string representation of a version number to an equivalent Version object.

Upvotes: 5

Rufus L
Rufus L

Reputation: 37020

You can use the Version class to parse and compare strings like that, for example:

string thisVersion = "0.3.9";
string newVersion = "0.4.0";

if (Version.Parse(newVersion) > Version.Parse(thisVersion))
{
    Console.WriteLine($"{newVersion} is greater than {thisVersion}");
}

Output

enter image description here

Upvotes: 5

Luthfi
Luthfi

Reputation: 478

.NET Framework comes with System.Version class that allows you to compare between version number.

var newVersion = new Version("0.3.9");
var oldVersion = new Version("0.3.8");

if(oldVersion < newVersion)
{
   //do something..
}

Upvotes: 3

Related Questions