C# 连接FTP 时 关于路径的问题
2016-06-17 08:37
525 查看
今天在开发项目时,需要连接FTP获取文件,其中关键的一步就是判断能否连接FTP以及FTP上的文件是否存在,判断的代码如下:
当 ftpServerFilePath 的路径为 “127.0.0.1\1.doc”, 这样进行传参时,就会抛异常,异常内容为无效的URi,如下图
这是因为FtpWebRequest.Create 连接时不能识别’\’ 这样的文件路径标识符,才会抛出上面的异常,因此传入的参数应该为”127.0.0.1/1.doc”。或者在方法里面进行替换。代码如下所示:
这样就不会跑异常,至于能否连接或者文件是否存在,请自行查看连接
https://msdn.microsoft.com/zh-cn/library/system.net.ftpstatuscode(v=vs.110).aspx
或者自行 google FtpStatusCode 即可。
那么修改后的代码为:(关于C# 连接完整的FTP 可以仔细 google 查询,网上多的是,这样就不累述了)
/// <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.1\1.doc”, 这样进行传参时,就会抛异常,异常内容为无效的URi,如下图
这是因为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 查询,网上多的是,这样就不累述了)
/// <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; }
相关文章推荐
- RAML用户应遵循的C#与Web API代码生成模式
- C#使用结构体,输入5个人的学号,姓名,分数,按照成绩高低排列打印出来
- 学习笔记之C# 子窗口调用父窗口变量
- C#之多态
- 关于C#中的new的用法
- C#获取运行程序的进程ID
- c#CheckedListBox单选多选个数限制问题
- C# 读取快捷方式指向的文件
- 转 C# 装箱和拆箱[整理]
- c#checkedListBox设置限制选项个数的问题
- C#MVC中Controler的自定义属性使用
- 练习、C# 结构体、冒泡排序
- C#中的代理
- C# 生成随机索引列表
- 关于C#对图片部分操作(水印、透明度)
- U3D结合第三方库ICSharpCode.SharpZipLib 实现文件夹打包解包
- C# UDP收发请求工具类
- C# 多线程是否结束可通过线程池可以判断
- VS2015中C#版本6.0的新特性 你需要知道
- C#中lambda表达式浅析