鄂尔多斯-招源科技

FileExistsHelper.cs 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace CallCenter.Utility
  8. {
  9. public class FileExistsHelper
  10. {
  11. #region 判断远程文件是否存在
  12. /// <summary>
  13. /// 判断远程文件是否存在
  14. /// </summary>
  15. /// <param name="fileUrl"></param>
  16. /// <returns></returns>
  17. public static bool RemoteFileExists(string fileUrl)
  18. {
  19. HttpWebRequest re = null;
  20. HttpWebResponse res = null;
  21. try
  22. {
  23. re = (HttpWebRequest)WebRequest.Create(fileUrl);
  24. res = (HttpWebResponse)re.GetResponse();
  25. if (res.ContentLength != 0)
  26. {
  27. //MessageBox.Show("文件存在");
  28. return true;
  29. }
  30. }
  31. catch (Exception)
  32. {
  33. //MessageBox.Show("无此文件");
  34. return false;
  35. }
  36. finally
  37. {
  38. if (re != null)
  39. {
  40. re.Abort();//销毁关闭连接
  41. }
  42. if (res != null)
  43. {
  44. res.Close();//销毁关闭响应
  45. }
  46. }
  47. return false;
  48. }
  49. #endregion
  50. }
  51. }