Home > Backend Development > C#.Net Tutorial > C# Determine whether remote file exists

C# Determine whether remote file exists

黄舟
Release: 2016-12-27 13:51:14
Original
1714 people have browsed it

#region 判断远程文件是否存在
       /// <summary>
       /// 判断远程文件是否存在
       /// </summary>
       ///<param name="fileUrl">
       /// <returns></returns>
       public static bool RemoteFileExists(string fileUrl)
       {
           HttpWebRequest re = null;
           HttpWebResponse res = null;
           try
           {
               re = (HttpWebRequest)WebRequest.Create(fileUrl);
               res = (HttpWebResponse)re.GetResponse();
               if (res.ContentLength != 0)
               {
                   //MessageBox.Show("文件存在");
                   return true;
               }
           }
           catch (Exception)
           {
               //MessageBox.Show("无此文件");
               return false;
           }
           finally
           {
               if (re != null)
               {
                   re.Abort();//销毁关闭连接
               }
               if (res != null)
               {
                   res.Close();//销毁关闭响应
               }
           }
           return false;
       }
       #endregion
Copy after login

The above is the content of C# to determine whether the remote file exists. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template