c#实现FTP方法(一)-FtpWebRequest.doc

上传人:枫** 文档编号:545537225 上传时间:2023-03-06 格式:DOC 页数:15 大小:54KB
返回 下载 相关 举报
c#实现FTP方法(一)-FtpWebRequest.doc_第1页
第1页 / 共15页
c#实现FTP方法(一)-FtpWebRequest.doc_第2页
第2页 / 共15页
c#实现FTP方法(一)-FtpWebRequest.doc_第3页
第3页 / 共15页
c#实现FTP方法(一)-FtpWebRequest.doc_第4页
第4页 / 共15页
c#实现FTP方法(一)-FtpWebRequest.doc_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《c#实现FTP方法(一)-FtpWebRequest.doc》由会员分享,可在线阅读,更多相关《c#实现FTP方法(一)-FtpWebRequest.doc(15页珍藏版)》请在金锄头文库上搜索。

1、c#实现FTP方法(一)-FtpWebRequestclass FTP_Class string ftpServerIP; string ftpUserID; string ftpPassword; FtpWebRequest reqFTP; public void Connecttest(string ftpServerIP, string ftpUserID, string ftpPassword) / 根据uri创建FtpWebRequest对象 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftp:/ + ftpServerI

2、P); / 指定数据传输类型 reqFTP.UseBinary = true; / ftp用户名和密码 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); #region 连接 / / 连接 / / private void Connect(String path)/连接ftp / 根据uri创建FtpWebRequest对象 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path); / 指定数据传输类型 reqFTP.UseBinary = tru

3、e; / ftp用户名和密码 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); #endregion #region ftp登录信息 / / ftp登录信息 / / ftpServerIP / ftpUserID / ftpPassword public void FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword) this.ftpServerIP = ftpServerIP; this.ftpUserID = ftpUser

4、ID; this.ftpPassword = ftpPassword; #endregion #region 获取文件列表 / / 获取文件列表 / / / / private string GetFileList(string path, string WRMethods)/上面的代码示例了如何从ftp服务器上获得文件列表 string downloadFiles; StringBuilder result = new StringBuilder(); try Connect(path); reqFTP.Method = WRMethods; WebResponse response = r

5、eqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);/中文文件名 string line = reader.ReadLine(); while (line != null) result.Append(line); result.Append(n); line = reader.ReadLine(); / to remove the trailing n result.Remove(result.ToString(

6、).LastIndexOf(n), 1); reader.Close(); response.Close(); return result.ToString().Split(n); catch (Exception ex) System.Windows.Forms.MessageBox.Show(ex.Message); downloadFiles = null; return downloadFiles; public string GetFileList(string path)/上面的代码示例了如何从ftp服务器上获得文件列表 return GetFileList(ftp:/ + ftp

7、ServerIP + / + path, WebRequestMethods.Ftp.ListDirectory); public string GetFileList()/上面的代码示例了如何从ftp服务器上获得文件列表 return GetFileList(ftp:/ + ftpServerIP + /, WebRequestMethods.Ftp.ListDirectory); #endregion #region 上传文件 / / 上传文件 / / public bool Upload(string filename, string path, out string errorinfo

8、) /上面的代码实现了从ftp服务器上载文件的功能 path = path.Replace(, /); FileInfo fileInf = new FileInfo(filename); string uri = ftp:/ + path + / + fileInf.Name; Connect(uri);/连接 / 默认为true,连接不会被关闭 / 在一个命令之后被执行 reqFTP.KeepAlive = false; / 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; / 上传文件时通知服务器文件的大小 reqFTP

9、.ContentLength = fileInf.Length; / 缓冲大小设置为kb int buffLength = 2048; byte buff = new bytebuffLength; int contentLen; / 打开一个文件流(System.IO.FileStream) 去读上传的文件 FileStream fs = fileInf.OpenRead(); try / 把上传的文件写入流 Stream strm = reqFTP.GetRequestStream(); / 每次读文件流的kb contentLen = fs.Read(buff, 0, buffLength); / 流内容没有结束 while (contentLen != 0) / 把内容从file stream 写入upload stream strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); / 关闭两个

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号