C#update - 代码阅读/ xml阅读器

时间:2013-05-02 20:58:36

标签: c#

我会添加什么,以便在没有可用更新时弹出一个消息框,说没有更新?虽然这可能是一个简单的解决方案,但我最近一直在研究很多更新系统,但我只是难以理解。

        Version newVersion = null;
        string url = "";
        XmlTextReader reader = null;
        try
        {
            string xmlURL = "URL";
            reader = new XmlTextReader(xmlURL);
            reader.MoveToContent();
            string elementName = "";
            if ((reader.NodeType == XmlNodeType.Element) &&
                (reader.Name == "App"))
            {
                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":
                                    url = reader.Value;
                                    break;
                            }
                        }
                    }
                }
            }
        }
        catch
        {
        }
        finally
        {
            if (reader != null) reader.Close();
        }
        Version curVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
        if (curVersion.CompareTo(newVersion) < 0)
        {
            string title = "New Update Avaliable";
            string question = "Download Now?";
            if (DialogResult.Yes == MessageBoxEx.Show(this, question, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                Process.Start(url);
            }
        }

1 个答案:

答案 0 :(得分:1)

curVersionnewVersion进行比较是否有问题?

    if (curVersion == newVersion) {
        MessageBox.Show("No Update Needed");
    } else if (curVersion.CompareTo(newVersion) < 0)
    {
        string title = "New Update Avaliable";
        string question = "Download Now?";
        if (DialogResult.Yes == MessageBoxEx.Show(this, question, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
        {
            Process.Start(url);
        }
    }