熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

c#判斷遠程文件是否存在

2013-11-13 09:46:29  來源: .NET編程 

  //:
    public static bool IsExist(string uri)
            {
                HttpWebRequest req = null;
                HttpWebResponse res = null;
                try
                {
                    req = (HttpWebRequest)WebRequestCreate(uri);
                    reqMethod = HEAD;
                    reqTimeout = ;
                    res = (HttpWebResponse)reqGetResponse();
                    return (resStatusCode == HttpStatusCodeOK);
                }
                catch
                {
                    return false;
                }
                finally
                {
                    if (res != null)
                    {
                        resClose();
                        res = null;
                    }
                    if (req != null)
                    {
                        reqAbort();
                        req = null;
                    }
                }
            }

  //:

  private bool UrlExistsUsingXmlHttp(string url)
    {
      //注意此方法需要引用Msxmldll
      MSXMLXMLHTTP _xml();
      _xmlhttpopen(HEAD url false null null);
      _xmlhttpsend();
      return (_xmlhttpstatus == );
    }

  //:
    private bool UrlExistsUsingSockets(string url)
    {
      if (urlStartsWith(//)) url = urlRemove( //Length);
      try
      {
        SystemNetIPHostEntry ipHost =SystemNetDnsGetHostEntry(url);// SystemNetDnsResolve(url);
        return true;
      }
      catch (SystemNetSocketsSocketException se)
      {
        SystemDiagnosticsTraceWrite(seMessage);
        return false;
      }
    }


From:http://tw.wingwit.com/Article/program/net/201311/11555.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.