2016-06-30 27 views
-1

基本上在這段代碼中,我正在檢查xyz(dotcom)/update.xml是否有可用的新版本,如果新版本可用,它將從網站。這是第一次工作,現在每次我檢查更新,它直接發送到「應用程序是最新的」代碼儘管有一個新的版本可用的XML文件,我相信我的程序沒有得到新的更新XML來自鏈接的文件,可能是什麼問題?請檢查下面的代碼。****如果您需要更多信息,請告訴我。我的C#程序沒有從網站獲取最新的XML文件內容

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

      } 
     } 
    } 
} 
+1

檢查請求是否被緩存 – toroveneno

+0

此代碼是相當'UGLY'並應細分爲更好的可讀性更小的方法。你有沒有嘗試通過調試器這個混亂..? – MethodMan

+0

請看看我的終極塊,我懷疑那裏的問題,開放塊開始和最後關閉......這可能是問題? –

回答

0

解決了這個問題。

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

在網上找到某個地方,請解釋一下,這裏發生了什麼?

和我finally塊是不正確的,應該是

finally 
      { 
       if (reader != null) 
       { 

        reader.Close(); 
       } 

      } 
+1

基本上,您正在向資源發出獲取請求。出於性能原因 - 服務器和瀏覽器緩存靜態內容。通過引入Guid.NewGuid() - 您對資源的請求每次都會變得不同 - 這意味着服務器將首先在緩存中查找它 - 但它不存在 - 因此它會提取新副本。這個?查詢字符串末尾的參數不是分辨率的一部分 - 因此它們被忽略。 – JDBennett

+0

謝謝! 希望這將永久解決問題。 –

相關問題