资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#连接FTP时路径出现问题怎么办

小编给大家分享一下C#连接FTP时路径出现问题怎么办,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

创新互联主要从事成都做网站、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务四川,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

最近在工作中遇到一个需求,需要利用C#连接FTP,在连接过程中遇到一个问题,所以下面这篇文章主要给大家介绍了关于C#连接FTP时路径问题的解决方法,需要的朋友可以参考借鉴,下面来一起看看吧。

今天在开发项目时,需要连接FTP获取文件,其中关键的一步就是判断能否连接FTP以及FTP上的文件是否存在

判断的代码如下:

/// 
  /// 测试是否可以成功连接FTP和判断文件是否存在
  /// 
  /// FTP上文件地址
  /// FTP登陆用户名
  /// FTP登陆密码
  /// 返回错误消息
  /// 
  private bool IsCanConnectFtp(string ftpServerFilePath, string ftpUserId, string ftpPwd, out string errorMsg)
  {
   bool flag = true;
   FtpWebResponse ftpResponse = null;
   FtpWebRequest ftpRequest = null;
   errorMsg = string.Empty;
   try
   {
    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerFilePath));
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    ftpRequest.Timeout = 2 * 1000;//超时时间设置为2秒。
    ftpRequest.Credentials = new NetworkCredential(ftpUserId, ftpPwd);
    ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
   }
   catch (WebException exception)
   {
    ftpResponse = (FtpWebResponse)exception.Response;
    switch (ftpResponse.StatusCode)
    {
     case FtpStatusCode.ActionNotTakenFileUnavailable:
      errorMsg = "下载的文件不存在";
      break;
     case FtpStatusCode.ActionNotTakenFileUnavailableOrBusy:
      errorMsg = "下载的文件正在使用,请稍后再试";
      break;
     default:
      errorMsg = "发生未知错误";
      break;
    }
    flag = false;
   }
   catch
   {
    errorMsg = "网络连接发生错误,请稍后再试";
    flag = true;
   }
   finally
   {
    if (ftpResponse != null)
    {
     ftpResponse.Close();
    }
   }
   return flag;
  }

当 ftpServerFilePath 的路径为 “127.0.0.1\1.doc”, 这样进行传参时,就会抛异常,异常内容为无效的URi,如下图

C#连接FTP时路径出现问题怎么办

解决方法

这是因为FtpWebRequest.Create 连接时不能识别'\' 这样的文件路径标识符,才会抛出上面的异常,因此传入的参数应该为”127.0.0.1/1.doc”。或者在方法里面进行替换。代码如下所示:

 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerFilePath.Replace("\\","/")));

这样就不会跑异常,至于能否连接或者文件是否存在,请自行查看连接

https://msdn.microsoft.com/zh-cn/library/system.net.ftpstatuscode(v=vs.110).aspx

或者自行 google FtpStatusCode 即可。

那么修改后的代码为:(关于C# 连接完整的FTP 可以仔细 google 查询,网上多的是,这样就不累述了)

 /// 
  /// 测试是否可以成功连接FTP和判断文件是否存在
  /// 
  /// FTP上文件地址
  /// FTP登陆用户名
  /// FTP登陆密码
  /// 返回错误消息
  /// 
  private bool IsCanConnectFtp(string ftpServerFilePath, string ftpUserId, string ftpPwd, out string errorMsg)
  {
   bool flag = true;
   FtpWebResponse ftpResponse = null;
   FtpWebRequest ftpRequest = null;
   errorMsg = string.Empty;
   try
   {
    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerFilePath.Replace("\\","/")));
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    ftpRequest.Timeout = 2 * 1000;//超时时间设置为2秒。
    ftpRequest.Credentials = new NetworkCredential(ftpUserId, ftpPwd);
    ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
   }
   catch (WebException exception)
   {
    ftpResponse = (FtpWebResponse)exception.Response;
    switch (ftpResponse.StatusCode)
    {
     case FtpStatusCode.ActionNotTakenFileUnavailable:
      errorMsg = "下载的文件不存在";
      break;
     case FtpStatusCode.ActionNotTakenFileUnavailableOrBusy:
      errorMsg = "下载的文件正在使用,请稍后再试";
      break;
     default:
      errorMsg = "发生未知错误";
      break;
    }
    flag = false;
   }
   catch
   {
    errorMsg = "网络连接发生错误,请稍后再试";
    flag = true;
   }
   finally
   {
    if (ftpResponse != null)
    {
     ftpResponse.Close();
    }
   }
   return flag;
  }

看完了这篇文章,相信你对C#连接FTP时路径出现问题怎么办有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


标题名称:C#连接FTP时路径出现问题怎么办
URL链接:http://cdkjz.cn/article/gcoihc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220