• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#版ftp方法实现类的代码

C#版ftp方法实现类的代码

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c#ftp上传,c#ftp客户端源代码,c#连接ftp,c#实现ftp客户端,c#读取ftp文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
/* 
FTPFactory.cs 
Better view with tab space=4 
Written by Jaimon Mathew (jaimonmathew@rediffmail.com) 
Rolander,Dan (Dan.Rolander@marriott.com) has modified the 
download 
method to cope with file name with path information. He also 
provided 
the XML comments so that the library provides Intellisense 
descriptions. 
use the following line to compile 
csc /target:library /out:FTPLib.dll /r:System.DLL FTPFactory.cs 
*/ 
using System; 
using System.Threading; 
using System.Net; 
using System.IO; 
using System.Text; 
using System.Net.Sockets; 
using System.Configuration; 
namespace AudioCollect 
{ 
/// <summary> 
/// FTPFactory 的摘要说明。 
/// </summary> 
public class FTPFactory 
{ 
static readonly log4net.ILog log = log4net.LogManager.GetLogger("log4net"); 
private string 
remoteHost,remotePath,remoteUser,remotePass,mes; 
private int remotePort,bytes; 
private Socket clientSocket; 
private int retValue; 
private Boolean debug; 
private Boolean logined; 
private string reply; 
private static int BLOCK_SIZE = 512; 
Byte[] buffer = new Byte[BLOCK_SIZE]; 
Encoding ASCII = Encoding.ASCII; 
public FTPFactory() 
{ 
string FTPRemoteIP = ConfigurationSettings.AppSettings["FTPRemoteIP"]; 
int FTPRemotePort = Convert.ToInt32( ConfigurationSettings.AppSettings["FTPRemotePort"] ); 
string FTPUser = ConfigurationSettings.AppSettings["FTPUser"]; 
string FTPPassword = ConfigurationSettings.AppSettings["FTPPassword"]; 
remoteHost = FTPRemoteIP; 
remotePath = "."; 
remoteUser = FTPUser; 
remotePass = FTPPassword; 
remotePort =FTPRemotePort; 
debug = false; 
logined = false; 
} 
/// 
/// Set the name of the FTP server to connect to. 
/// 
/// Server name 
public void setRemoteHost(string remoteHost) 
{ 
this.remoteHost = remoteHost; 
} 
/// 
/// Return the name of the current FTP server. 
/// 
/// Server name 
public string getRemoteHost() 
{ 
return remoteHost; 
} 
/// 
/// Set the port number to use for FTP. 
/// 
/// Port number 
public void setRemotePort(int remotePort) 
{ 
this.remotePort = remotePort; 
} 
/// 
/// Return the current port number. 
/// 
/// Current port number 
public int getRemotePort() 
{ 
return remotePort; 
} 
/// 
/// Set the remote directory path. 
/// 
/// The remote directory path 
public void setRemotePath(string remotePath) 
{ 
this.remotePath = remotePath; 
} 
/// 
/// Return the current remote directory path. 
/// 
/// The current remote directory path. 
public string getRemotePath() 
{ 
return remotePath; 
} 
/// 
/// Set the user name to use for logging into the remote server. 
/// 
/// Username 
public void setRemoteUser(string remoteUser) 
{ 
this.remoteUser = remoteUser; 
} 
/// 
/// Set the password to user for logging into the remote server. 
/// 
/// Password 
public void setRemotePass(string remotePass) 
{ 
this.remotePass = remotePass; 
} 
/// 
/// Return a string array containing the remote directory's file list. 
/// 
/// 
/// 
public string[] getFileList(string mask) 
{ 
if(!logined) 
{ 
login(); 
} 
Socket cSocket = createDataSocket(); 
sendCommand("NLST " + mask); 
if(!(retValue == 150 || retValue == 125)) 
{ 
throw new IOException(reply.Substring(4)); 
} 
mes = ""; 
Thread.Sleep(700); 
while(true) 
{ 
if(cSocket.Connected) 
{ 
int bytes = cSocket.Receive(buffer, buffer.Length, 0); 
mes += ASCII.GetString(buffer, 0, bytes); 
if(bytes < buffer.Length) 
{ 
break; 
} 
} 
else 
{ 
log.Info("socket 连接断了!"); 
} 
} 
log.Info(mes); 
char[] seperator = {'\n'}; 
string[] mess = mes.Split(seperator); 
foreach(string fileName in mess) 
{ 
log.Info(fileName); 
} 
cSocket.Close(); 
readReply(); 
if(retValue != 226) 
{ 
throw new IOException(reply.Substring(4)); 
} 
return mess; 
} 
public string[] getFileList() 
{ 
if(!logined) 
{ 
login(); 
} 
Socket cSocket = createDataSocket(); 
sendCommand("LIST "); 
if(!(retValue == 150 || retValue == 125)) 
{ 
throw new IOException(reply.Substring(4)); 
} 
mes = ""; 
while(true) 
{ 
int bytes = cSocket.Receive(buffer, buffer.Length, 0); 
mes += ASCII.GetString(buffer, 0, bytes); 
if(bytes < buffer.Length) 
{ 
break; 
} 
} 
log.Info(mes); 
char[] seperator = {'\n'}; 
string[] mess = mes.Split(seperator); 
cSocket.Close(); 
readReply(); 
if(retValue != 226) 
{ 
throw new IOException(reply.Substring(4)); 
} 
return mess; 
} 
/// 
/// Return the size of a file. 
/// 
/// 
/// 
public long getFileSize(string fileName) 
{ 
if(!logined) 
{ 
login(); 
} 
sendCommand("SIZE " +&nb
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#开发教程之ftp操作方法整理
  • C#实现自定义FTP操作封装类实例
  • C#操作FTP出现500错误解决办法
  • c#操作ftp类分享
  • C#版ftp方法实现类的代码

相关文章

  • 2017-05-28c# 可疑文件扫描代码(找到木马)(简)
  • 2017-05-28C#读取视频的宽度和高度等信息的方法
  • 2017-05-28python实现AutoResetEvent类的阻塞模式方法解析
  • 2017-05-28C#计算字符串相似性的方法
  • 2017-05-28c# 开机启动项的小例子
  • 2017-05-28C#灰度化图像的实例代码
  • 2017-05-28C#使用Streamwriter打开文件的方法
  • 2017-05-28Response.Redirect 正在中止线程解决方案
  • 2017-05-28C#打印类PrintDocument、PrintDialog、PrintPreviewDialog使用示例
  • 2017-05-28.Net常见问题之C#中的委托

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C# 判断字符串第一位是否为数字
    • 常用C#正则表达式汇总介绍
    • 浅谈C#手机号换成111XXXX1111 这种显示的解决思路
    • C#利用win32 Api 修改本地系统时间、获取硬盘序列号
    • C#文件加密方法汇总
    • C#实现的鼠标钩子
    • C#通过xpath查找xml指定元素的方法
    • C#微信开发之获取接口调用凭据
    • C#实现判断一个时间点是否位于给定时间区间的方法
    • C#实现线程池的简单示例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有