2013-05-02 36 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); 
     } 
    }