我是靠谱客的博主 甜蜜月饼,这篇文章主要介绍C#连接FTP时路径出现问题的解决方法,现在分享给大家,希望可以做个参考。

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

前言

本文主要给大家介绍了关于C#连接FTP时路径问题的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍:

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

判断的代码如下:


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// <summary> /// 测试是否可以成功连接FTP和判断文件是否存在 /// </summary> /// <param name="ftpServerFilePath">FTP上文件地址</param> /// <param name="ftpUserId">FTP登陆用户名</param> /// <param name="ftpPwd">FTP登陆密码</param> /// <param name="errorMsg">返回错误消息</param> /// <returns></returns> 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.11.doc”, 这样进行传参时,就会抛异常,异常内容为无效的URi,如下图

解决方法

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


复制代码
1
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 查询,网上多的是,这样就不累述了)


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// <summary> /// 测试是否可以成功连接FTP和判断文件是否存在 /// </summary> /// <param name="ftpServerFilePath">FTP上文件地址</param> /// <param name="ftpUserId">FTP登陆用户名</param> /// <param name="ftpPwd">FTP登陆密码</param> /// <param name="errorMsg">返回错误消息</param> /// <returns></returns> 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时路径出现问题的解决方法的全部内容,更多相关C#连接FTP时路径出现问题内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(101)

评论列表共有 0 条评论

立即
投稿
返回
顶部