日期:2014-05-20  浏览次数:20738 次

在 winfrom中 不显示删除回复显示所有回复显示星级回复显示得分回复 在用DownloadFile下载图片前,如何用程序判断远端图片是否存在?
是在[color=#FF0000]winfrom[/color]中  



网上很多的方法都是asp.net中的 在winfrom中行不通 高手指点 谢谢诶

------解决方案--------------------
判断网络地址 Url 是否存在的方法
C# code
private bool UrlIsExist(String url)
{
  System.Uri u = null;
  try
  {
    u = new Uri(url);
  }
  catch { return false; }
  bool isExist = false;
  System.Net.HttpWebRequest r = System.Net.HttpWebRequest.Create(u) as System.Net.HttpWebRequest;
  r.Method = "HEAD";
  try
  {
    System.Net.HttpWebResponse s = r.GetResponse() as System.Net.HttpWebResponse;
    if (s.StatusCode == System.Net.HttpStatusCode.OK)
    {
      isExist = true;
    }
  }
  catch (System.Net.WebException x)
  {
    try
    {
      isExist = ((x.Response as System.Net.HttpWebResponse).StatusCode != System.Net.HttpStatusCode.NotFound);
    }
    catch { isExist = (x.Status == System.Net.WebExceptionStatus.Success); }
  }
  return isExist;
}