代码语言
.
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类
作者:
CSDN
/ 发布于
2012/1/11
/
521
<div><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)"> FtpFile { </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> ftpServerIP; </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> FtpServerIP { </span><span style="color: rgb(0,0,255)">get</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> ftpServerIP; } </span><span style="color: rgb(0,0,255)">set</span><span style="color: rgb(0,0,0)"> { ftpServerIP </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> value; } } </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> ftpUserID; </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> FtpUserID { </span><span style="color: rgb(0,0,255)">get</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> ftpUserID; } </span><span style="color: rgb(0,0,255)">set</span><span style="color: rgb(0,0,0)"> { ftpUserID </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> value; } } </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> ftpPassword; </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> FtpPassword { </span><span style="color: rgb(0,0,255)">get</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> ftpPassword; } </span><span style="color: rgb(0,0,255)">set</span><span style="color: rgb(0,0,0)"> { ftpPassword </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> value; } } FtpWebRequest reqFTP; </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> FtpServer </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> System.Configuration.ConfigurationSettings.AppSettings[</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">FtpServer</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">]; </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> FtpUser </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> System.Configuration.ConfigurationSettings.AppSettings[</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">FtpUser</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">]; </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> FtpPwd </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> System.Configuration.ConfigurationSettings.AppSettings[</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">FtpPwd</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">]; </span><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> Connect(String path)</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">连接ftp</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 根据uri创建FtpWebRequest对象</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> reqFTP </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (FtpWebRequest)FtpWebRequest.Create(</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Uri(path)); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 指定数据传输类型</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> reqFTP.UseBinary </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> ftp用户名和密码</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> reqFTP.Credentials </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> NetworkCredential(ftpUserID, ftpPassword); } </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> FtpFile(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> ftpServerIP, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> ftpUserID, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> ftpPassword) { </span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.ftpServerIP </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> ftpServerIP; </span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.ftpUserID </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> ftpUserID; </span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.ftpPassword </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> ftpPassword; } </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> FtpFile() { </span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.ftpServerIP </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> FtpServer; </span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.ftpUserID </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> FtpUser; </span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.ftpPassword </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> FtpPwd; } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">都调用这个</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] GetFileList(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> path, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> WRMethods)</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">上面的代码示例了如何从ftp服务器上获得文件列表</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] downloadFiles; StringBuilder result </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> StringBuilder(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { Connect(path); reqFTP.Method </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> WRMethods; WebResponse response </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> reqFTP.GetResponse(); StreamReader reader </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">中文文件名</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> line </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> reader.ReadLine(); </span><span style="color: rgb(0,0,255)">while</span><span style="color: rgb(0,0,0)"> (line </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">) { result.Append(line); result.Append(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">\n</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); line </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> reader.ReadLine(); } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> to remove the trailing '\n'</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> result.Remove(result.ToString().LastIndexOf(</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">\n</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">), </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">); reader.Close(); response.Close(); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> result.ToString().Split(</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">\n</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">); } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); downloadFiles </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> downloadFiles; } } </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] GetFileList(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> path)</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">上面的代码示例了如何从ftp服务器上获得文件列表</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> GetFileList(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">ftp://</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> ftpServerIP </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> path, WebRequestMethods.Ftp.ListDirectory); } </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] GetFileList()</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">上面的代码示例了如何从ftp服务器上获得文件列表</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> GetFileList(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">ftp://</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> ftpServerIP </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">/</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, WebRequestMethods.Ftp.ListDirectory); } </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> Upload(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> filedir,</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> filename) </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">上面的代码实现了从ftp服务器上载文件的功能</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> { FileInfo fileInf </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> FileInfo(filename); </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> uri </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">ftp://</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> ftpServerIP </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> filedir </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> fileInf.Name; Connect(uri);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">连接 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 默认为true,连接不会被关闭 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 在一个命令之后被执行</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> reqFTP.KeepAlive </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 指定执行什么命令</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> reqFTP.Method </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> WebRequestMethods.Ftp.UploadFile; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 上传文件时通知服务器文件的大小</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> reqFTP.ContentLength </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> fileInf.Length; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 缓冲大小设置为kb</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> buffLength </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2048</span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(128,0,128)">5</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[] buff </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[buffLength]; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> contentLen; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 打开一个文件流(System.IO.FileStream) 去读上传的文件</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> FileStream fs </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> fileInf.OpenRead(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 把上传的文件写入流</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> Stream strm </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> reqFTP.GetRequestStream(); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 每次读文件流的kb</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> contentLen </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> fs.Read(buff, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, buffLength); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 流内容没有结束</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">while</span><span style="color: rgb(0,0,0)"> (contentLen </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 把内容从file stream 写入upload stream</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> strm.Write(buff, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, contentLen); contentLen </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> fs.Read(buff, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, buffLength); } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 关闭两个流</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> strm.Close(); fs.Close(); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">""</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Upload Error</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> ex.Message; } } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 下载文件 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="filedir"></span><span style="color: rgb(0,128,0)">文件夹地址:必须是服务器根级相对地址,比如"/ftpFiles/images"</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="filePath"></span><span style="color: rgb(0,128,0)">下载文件新路径,但不包含文件名,如:D:\Photo</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="fileName"></span><span style="color: rgb(0,128,0)">ftp文件绝对路径,如:ftp://127.0.0.1/Photo/1.bmp</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> Download(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> filedir,</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> filePath, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> fileName)</span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)">/上面的代码实现了从ftp服务器下载文件的功能</span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { String onlyFileName </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Path.GetFileName(fileName); </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> newFileName </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> filePath </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">\\</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> onlyFileName; </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (File.Exists(newFileName)) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">本地文件</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> newFileName </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">已存在,无法下载</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> url </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">ftp://</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> ftpServerIP </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> filedir </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> onlyFileName; Connect(url);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">连接 </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> reqFTP.Credentials </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse response </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> response.GetResponseStream(); </span><span style="color: rgb(0,0,255)">long</span><span style="color: rgb(0,0,0)"> cl </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> response.ContentLength; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> bufferSize </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2048</span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(128,0,128)">5</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> readCount; </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[] buffer </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[bufferSize]; readCount </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> ftpStream.Read(buffer, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, bufferSize); FileStream outputStream </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> FileStream(newFileName, FileMode.Create); </span><span style="color: rgb(0,0,255)">while</span><span style="color: rgb(0,0,0)"> (readCount </span><span style="color: rgb(0,0,0)">></span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">) { outputStream.Write(buffer, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, readCount); readCount </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> ftpStream.Read(buffer, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">""</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">因</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> ex.Message </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">,无法下载</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; } } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">删除文件</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> DeleteFileName(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> filedir,</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> fileName) { </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> fName</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Path.GetFileName(fileName); </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> uri </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">ftp://</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> ftpServerIP </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> filedir</span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)">fName; Connect(uri);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">连接 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 默认为true,连接不会被关闭 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 在一个命令之后被执行</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> reqFTP.KeepAlive </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> 指定执行什么命令</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> reqFTP.Method </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> WebRequestMethods.Ftp.DeleteFile; FtpWebResponse response </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (FtpWebResponse)reqFTP.GetResponse(); response.Close(); } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { MessageBox.Show(ex.Message, </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">删除错误</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); } } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">创建目录</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> MakeDir(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> dirName) { </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> uri </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">ftp://</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> ftpServerIP </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">/</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> dirName; Connect(uri);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">连接 </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> reqFTP.Method </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> WebRequestMethods.Ftp.MakeDirectory; FtpWebResponse response </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (FtpWebResponse)reqFTP.GetResponse(); response.Close(); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">""</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (WebException ex) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">[Make Dir]</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> ex.Message; } } </span></div>
试试其它关键字
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转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
CSDN
贡献的其它代码
(
27
)
.
xml操作类
.
完全无组件,不用调用Excel.exe 导出数据到Excel
.
DropDownList手动构造一个下拉树形
.
判断是否低于IE6的版本
.
javascript 鼠标拖拉画图
.
取汉字首字母
.
字符串格式化通用类
.
mssql海量高效分页存储过程
.
.net 获取远程IP 得到省市
.
javascript 模仿windows拖动 封装类
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3