-1

Basically in this code I'm checking if the xyz(dotcom)/update.xml has a new version available, if the new version is available it will download from the website. It was working for first time, now everytime I check for update, it directly just sent to "Application is Up-to-Date" code despite there is a new version available in xml file, I believe my program is not getting new updated xml file from the link, what could be the issue? please check code below.**** If you need more information, please let me know.

public void checkForUpdate()
{    
        string download_url = "";
        Version newVersion = null;
        string xmlurl = "http://xyz.(dotcom)/update.xml";

        try
        {
            XmlTextReader reader = new XmlTextReader(xmlurl);
            reader.MoveToContent();
            string elementname = "";

            if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "XYZ"))
            {
                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":

                                    download_url = reader.Value;
                                    break;
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
        finally
        {
            if (reader != null)
            {

                reader.Close();
            }

            Version Applicationversion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            if (Applicationversion.CompareTo(newVersion) < 0)
            {
                DialogResult dialogresult = (MessageBox.Show("New Version: " + newVersion + " is available to download, would you like to download now?", "New Version Available", MessageBoxButtons.YesNo, MessageBoxIcon.Information));
                {

                    if (dialogresult == DialogResult.Yes)
                    {
                        //System.Diagnostics.Process.Start(link);
                        Download dw = new Download();
                        dw.ShowDialog();
                    }
                    else if (dialogresult == DialogResult.No)
                    {

                    }
                }
            }

            else
            {
                MessageBox.Show("Your application is up-to-date!", "Up-to-Date!", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
    }
}
Fawad Naseer
  • 35
  • 10

1 Answers1

0

this fixed the issue.

XmlTextReader reader = new XmlTextReader(xmlurl + "?" + Guid.NewGuid().ToString());

found somewhere on the internet, please explain, what is going on here?

and my finally block wasn't correct, it should be

 finally
            {
                if (reader != null)
                {

                    reader.Close();
                }

            }
Fawad Naseer
  • 35
  • 10
  • 1
    Basically you are issuing a get request to a resource. For performance reasons - servers and browsers cache static content. By introducing the Guid.NewGuid() - your request for a resource becomes different every time - meaning the server will look for it first in cache - but it doesn't exist - so it fetches a new copy. The ? parameters at the end of the query string are not part of resolution - so they are ignored. – JDBennett Jun 30 '16 at 16:46