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

  介绍

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

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

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

<强>判断的代码如下:

///& lt; summary>///测试是否可以成功连接FTP和判断文件是否存在///& lt;/summary>///& lt;参数name=癴tpServerFilePath"在FTP上文件地址& lt;/param>///& lt;参数name=癴tpUserId"在FTP登陆用户名& lt;/param>///& lt;参数name=癴tpPwd"在FTP登陆密码& lt;/param>///& lt;参数name=癳rrorMsg"在返回错误消息& lt;/param>///& lt; returns> & lt;/returns>
  私人bool IsCanConnectFtp (ftpPwd ftpUserId ftpServerFilePath的字符串,字符串,字符串,字符串errorMsg)
  {
  bool国旗=true;
  FtpWebResponse ftpResponse=零;
  FtpWebRequest ftpRequest=零;
  errorMsg=string.Empty;
  试一试
  {
  FtpWebRequest ftpRequest=(FtpWebRequest)。新建(Uri (“ftp://"+ ftpServerFilePath));
  ftpRequest。方法=WebRequestMethods.Ftp.ListDirectory;
  ftpRequest。Timeout=2 * 1000;//超时时间设置为2秒。
  ftpRequest。凭证=new NetworkCredential (ftpUserId ftpPwd);
  ftpResponse=(FtpWebResponse) ftpRequest.GetResponse ();
  }
  抓住(WebException例外)
  {
  ftpResponse=(FtpWebResponse) exception.Response;
  开关(ftpResponse.StatusCode)
  {
  案例FtpStatusCode.ActionNotTakenFileUnavailable:
  errorMsg=跋略氐奈募淮嬖凇?
  打破;
  案例FtpStatusCode.ActionNotTakenFileUnavailableOrBusy:
  errorMsg=跋略氐奈募谑褂?请稍后再试,;
  打破;
  默认值:
  errorMsg=胺⑸粗砦蟆?
  打破;
  }
  国旗=false;
  }
  抓
  {
  errorMsg=巴缌臃⑸砦?请稍后再试,;
  国旗=true;
  }
  最后
  {
  如果(ftpResponse !=null)
  {
  ftpResponse.Close ();
  }
  }
  返回国旗;
  }

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

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

这是因为<代码> FtpWebRequest。创建连接时不能识别& # 39;\ & # 39;这样的文件路径标识符,才会抛出上面的异常,因此传入的参数应该为“127.0.0.1/1。医生”。或者在方法里面进行替换。代码如下所示:

 ftpRequest=(FtpWebRequest) FtpWebRequest。新建(Uri (“ftp://"+ ftpServerFilePath.Replace (“\ \”,“/?)), 

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

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

或者自行谷歌FtpStatusCode即可。

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

///& lt; summary>///测试是否可以成功连接FTP和判断文件是否存在///& lt;/summary>///& lt;参数name=癴tpServerFilePath"在FTP上文件地址& lt;/param>///& lt;参数name=癴tpUserId"在FTP登陆用户名& lt;/param>///& lt;参数name=癴tpPwd"在FTP登陆密码& lt;/param>///& lt;参数name=癳rrorMsg"在返回错误消息& lt;/param>///& lt; returns> & lt;/returns>
  私人bool IsCanConnectFtp (ftpPwd ftpUserId ftpServerFilePath的字符串,字符串,字符串,字符串errorMsg)
  {
  bool国旗=true;
  FtpWebResponse ftpResponse=零;
  FtpWebRequest ftpRequest=零;
  errorMsg=string.Empty;
  试一试
  {
  FtpWebRequest ftpRequest=(FtpWebRequest)。新建(Uri (“ftp://"+ ftpServerFilePath.Replace (“\ \”,“/?));
  ftpRequest。方法=WebRequestMethods.Ftp.ListDirectory;
  ftpRequest。Timeout=2 * 1000;//超时时间设置为2秒。
  ftpRequest。凭证=new NetworkCredential (ftpUserId ftpPwd);
  ftpResponse=(FtpWebResponse) ftpRequest.GetResponse ();
  }
  抓住(WebException例外)
  {
  ftpResponse=(FtpWebResponse) exception.Response;
  开关(ftpResponse.StatusCode)
  {
  案例FtpStatusCode.ActionNotTakenFileUnavailable:
  errorMsg=跋略氐奈募淮嬖凇?
  打破;
  案例FtpStatusCode.ActionNotTakenFileUnavailableOrBusy:
  errorMsg=跋略氐奈募谑褂?请稍后再试,;
  打破;
  默认值:
  errorMsg=胺⑸粗砦蟆?
  打破;
  }
  国旗=false;
  }
  抓
  {
  errorMsg=巴缌臃⑸砦?请稍后再试,;
  国旗=true;
  }
  最后
  {
  如果(ftpResponse !=null)
  {
  ftpResponse.Close ();
  }
  }
  返回国旗;
  }

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