代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
对FTP的操作
作者:
dezai
/ 发布于
2014/1/7
/
813
C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Net; usingSystem.Data; usingSystem.IO; usingSystem.ComponentModel; namespaceCommon { publicclassFTPClient { privatestringftpServerIP=String.Empty; privatestringftpUser=String.Empty; privatestringftpPassword=String.Empty; privatestringftpRootURL=String.Empty; publicFTPClient(stringurl,stringuserid,stringpassword) { this.ftpServerIP=ftp的IP地址; this.ftpUser=用户名; this.ftpPassword=密码; this.ftpRootURL="ftp://"+url+"/"; } ///<summary> ///上传 ///</summary> ///<paramname="localFile">本地文件绝对路径</param> ///<paramname="ftpPath">上传到ftp的路径</param> ///<paramname="ftpFileName">上传到ftp的文件名</param> publicboolfileUpload(FileInfolocalFile,stringftpPath,stringftpFileName) { boolsuccess=false; FtpWebRequestftpWebRequest=null; FileStreamlocalFileStream=null; StreamrequestStream=null; try { stringuri=ftpRootURL+ftpPath+ftpFileName; ftpWebRequest=(FtpWebRequest)FtpWebRequest.Create(newUri(uri)); ftpWebRequest.Credentials=newNetworkCredential(ftpUser,ftpPassword); ftpWebRequest.UseBinary=true; ftpWebRequest.KeepAlive=false; ftpWebRequest.Method=WebRequestMethods.Ftp.UploadFile; ftpWebRequest.ContentLength=localFile.Length; intbuffLength=2048; byte[]buff=newbyte[buffLength]; intcontentLen; localFileStream=localFile.OpenRead(); requestStream=ftpWebRequest.GetRequestStream(); contentLen=localFileStream.Read(buff,0,buffLength); while(contentLen!=0) { requestStream.Write(buff,0,contentLen); contentLen=localFileStream.Read(buff,0,buffLength); } success=true; } catch(Exception) { success=false; } finally { if(requestStream!=null) { requestStream.Close(); } if(localFileStream!=null) { localFileStream.Close(); } } returnsuccess; } ///<summary> ///上传文件 ///</summary> ///<paramname="localPath">本地文件地址(没有文件名)</param> ///<paramname="localFileName">本地文件名</param> ///<paramname="ftpPath">上传到ftp的路径</param> ///<paramname="ftpFileName">上传到ftp的文件名</param> publicboolfileUpload(stringlocalPath,stringlocalFileName,stringftpPath,stringftpFileName) { boolsuccess=false; try { FileInfolocalFile=newFileInfo(localPath+localFileName); if(localFile.Exists) { success=fileUpload(localFile,ftpPath,ftpFileName); } else { success=false; } } catch(Exception) { success=false; } returnsuccess; } ///<summary> ///下载文件 ///</summary> ///<paramname="localPath">本地文件地址(没有文件名)</param> ///<paramname="localFileName">本地文件名</param> ///<paramname="ftpPath">下载的ftp的路径</param> ///<paramname="ftpFileName">下载的ftp的文件名</param> publicboolfileDownload(stringlocalPath,stringlocalFileName,stringftpPath,stringftpFileName) { boolsuccess=false; FtpWebRequestftpWebRequest=null; FtpWebResponseftpWebResponse=null; StreamftpResponseStream=null; FileStreamoutputStream=null; try { outputStream=newFileStream(localPath+localFileName,FileMode.Create); stringuri=ftpRootURL+ftpPath+ftpFileName; ftpWebRequest=(FtpWebRequest)FtpWebRequest.Create(newUri(uri)); ftpWebRequest.Credentials=newNetworkCredential(ftpUser,ftpPassword); ftpWebRequest.UseBinary=true; ftpWebRequest.Method=WebRequestMethods.Ftp.DownloadFile; ftpWebResponse=(FtpWebResponse)ftpWebRequest.GetResponse(); ftpResponseStream=ftpWebResponse.GetResponseStream(); longcontentLength=ftpWebResponse.ContentLength; intbufferSize=2048; byte[]buffer=newbyte[bufferSize]; intreadCount; readCount=ftpResponseStream.Read(buffer,0,bufferSize); while(readCount>0) { outputStream.Write(buffer,0,readCount); readCount=ftpResponseStream.Read(buffer,0,bufferSize); } success=true; } catch(Exception) { success=false; } finally { if(outputStream!=null) { outputStream.Close(); } if(ftpResponseStream!=null) { ftpResponseStream.Close(); } if(ftpWebResponse!=null) { ftpWebResponse.Close(); } } returnsuccess; } ///<summary> ///重命名 ///</summary> ///<paramname="ftpPath">ftp文件路径</param> ///<paramname="currentFilename"></param> ///<paramname="newFilename"></param> publicboolfileRename(stringftpPath,stringcurrentFileName,stringnewFileName) { boolsuccess=false; FtpWebRequestftpWebRequest=null; FtpWebResponseftpWebResponse=null; StreamftpResponseStream=null; try { stringuri=ftpRootURL+ftpPath+currentFileName; ftpWebRequest=(FtpWebRequest)FtpWebRequest.Create(newUri(uri)); ftpWebRequest.Credentials=newNetworkCredential(ftpUser,ftpPassword); ftpWebRequest.UseBinary=true; ftpWebRequest.Method=WebRequestMethods.Ftp.Rename; ftpWebRequest.RenameTo=newFileName; ftpWebResponse=(FtpWebResponse)ftpWebRequest.GetResponse(); ftpResponseStream=ftpWebResponse.GetResponseStream(); } catch(Exception) { success=false; } finally { if(ftpResponseStream!=null) { ftpResponseStream.Close(); } if(ftpWebResponse!=null) { ftpWebResponse.Close(); } } returnsuccess; } ///<summary> ///消除文件 ///</summary> ///<paramname="filePath"></param> publicboolfileDelete(stringftpPath,stringftpName) { boolsuccess=false; FtpWebRequestftpWebRequest=null; FtpWebResponseftpWebResponse=null; StreamftpResponseStream=null; StreamReaderstreamReader=null; try { stringuri=ftpRootURL+ftpPath+ftpName; ftpWebRequest=(FtpWebRequest)FtpWebRequest.Create(newUri(uri)); ftpWebRequest.Credentials=newNetworkCredential(ftpUser,ftpPassword); ftpWebRequest.KeepAlive=false; ftpWebRequest.Method=WebRequestMethods.Ftp.DeleteFile; ftpWebResponse=(FtpWebResponse)ftpWebRequest.GetResponse(); longsize=ftpWebResponse.ContentLength; ftpResponseStream=ftpWebResponse.GetResponseStream(); streamReader=newStreamReader(ftpResponseStream); stringresult=String.Empty; result=streamReader.ReadToEnd(); success=true; } catch(Exception) { success=false; } finally { if(streamReader!=null) { streamReader.Close(); } if(ftpResponseStream!=null) { ftpResponseStream.Close(); } if(ftpWebResponse!=null) { ftpWebResponse.Close(); } } returnsuccess; } ///<summary> ///文件存在检查 ///</summary> ///<paramname="ftpPath"></param> ///<paramname="ftpName"></param> ///<returns></returns> publicboolfileCheckExist(stringftpPath,stringftpName) { boolsuccess=false; FtpWebRequestftpWebRequest=null; WebResponsewebResponse=null; StreamReaderreader=null; try { stringurl=ftpRootURL+ftpPath; ftpWebRequest=(FtpWebRequest)FtpWebRequest.Create(newUri(url)); ftpWebRequest.Credentials=newNetworkCredential(ftpUser,ftpPassword); ftpWebRequest.Method=WebRequestMethods.Ftp.ListDirectory; ftpWebRequest.KeepAlive=false; webResponse=ftpWebRequest.GetResponse(); reader=newStreamReader(webResponse.GetResponseStream()); stringline=reader.ReadLine(); while(line!=null) { if(line==ftpName) { success=true; break; } line=reader.ReadLine(); } } catch(Exception) { success=false; } finally { if(reader!=null) { reader.Close(); } if(webResponse!=null) { webResponse.Close(); } } returnsuccess; } } }
试试其它关键字
FTP
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3