2016-06-30 26 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

谢谢! 希望这将永久解决问题。 –

相关问题