2013-05-02 39 views
-2

我会添加什么,以便如果没有更新可用消息框弹出说没有更新?虽然这可能是一个简单的解决方法,但最近我一直在研究很多更新系统,但是我只是为此而苦恼。C#更新 - 代码阅读/ xml阅读器

 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); 
      } 
     } 
+0

我不明白这个问题。比较版本时不能添加else分支,并在那里显示消息? – Patrick 2013-05-02 21:11:24

+0

但是你确实知道'else'关键字是如何工作的,对吧?如果没有更新,它将适用于其他分支,还是我没有得到这个? – Patrick 2013-05-02 21:30:43

+0

@Patrick是的,但我的代码设置为读取版本的方式我不能这样做,因为它然后读取版本不正确,并始终返回新的更新可用。 – 2013-05-02 21:36:33

回答

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); 
     } 
    }