开发者

C# Integer parsing/comparing

HttpWebRequest web;
        try
        {
            web = (HttpWebRequest)HttpWebRequest.Create("website.name");

            WebResponse Response = web.GetResponse();
            Stream WebStream = Response.GetResponseStream();
            StreamReader Reader = new StreamReader(WebStream);
            string data = Reader.ReadToEnd();

            Reader.Close();
            WebStream.Close();
            Response.Close();

            string[] ver = Regex.Split(data, "version=");

            if (int.Parse(ver[1]) == int.Parse(appVersion))
            {
                tss1.Text = "Status : You currently have the latest version";               
            }
           开发者_如何学Python else
            {
                tss1.Text ="Status : A new version of app is available.";
                System.Diagnostics.Process.Start("website.name");
            } 
        }
        catch (Exception ex)
        {
            tss1.Text = "Status : Update check failed.";
            Debug.Write(ex.ToString());
        }

I'm trying to use the above code to connect to webpage and pull down the latest version number for the app which it does just fine. My problem comes in when trying to compare the number found on the webpage to number provided by appVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();


It seems that you want to check if the version of the running program is lower than the version string you just downloaded.

Instead of using integer comparison you could just do Version comparison. Create a Version object from a string you download: var downloadedVersion = new Version(versionStringDownloadedFromWeb); and compare this to the assemblys version:

if (Assembly.GetExecutingAssembly().GetName().Version < downloadedVersion) 
{
  // Your version is outdated!
}

You can do this since the Version objects implements the IComparable interface.


Assembly.GetExecutingAssembly().GetName().Version.ToString(); will be of the format "x.y.z.q" and that cannot be parsed as an integer. try

var appVersion = Assembly.GetExecutingAssembly().GetName().Version.Major;

which will give you the major version number as an int

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜