c#上传下载ftp(⽀持断点续传)这个ftpClient是从⽹上来的,⾃⼰加了断点续传的⽅法
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;
namespace ftpGet
{
///<summary>
/// FTP Client
///</summary>
public class FTPClient
{
构造函数
登陆字段、属性
链接
传输模式
⽂件操作
上传和下载
⽬录操作
内部变量
内部函数
}
}
当然,⼤家还要看看Main⽅法
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ftpGet
{
class Program
{
static string remotingFolder = System.Configuration.ConfigurationSettings.AppSettings["remotingFolder"];  //远程ftp⽂件⽬录static string localFolder = System.Configuration.ConfigurationSettings.AppSettings["localFolder"];  //要下载到的本地⽬录static string ftpServer = System.Configuration.ConfigurationSettings.AppSettings["ftpServer"];  //ftp服务器
static string user = System.Configuration.ConfigurationSettings.AppSettings["user"];  //⽤户名
static string pwd = System.Configuration.ConfigurationSettings.AppSettings["pwd"];  //密码
static string port = System.Configuration.ConfigurationSettings.AppSettings["port"];  //端⼝
static void Main(string[] args)
{
connect下载FTPClient client = new FTPClient(ftpServer, "/", user, pwd, int.Parse(port));
client.Connect();
GetFolder("*", remotingFolder, client, CreateFolder());
client.DisConnect();
ClearFolder();
Console.WriteLine("下载完毕");
System.Threading.Thread.Sleep(3000);
}
///<summary>
///在本地⽬录下创建⼀个以⽇期为名称的⽬录,我做这个ftp的主要⽬的是为了每天都备份
///</summary>
///<returns>创建的⽬录名</returns>
private static string CreateFolder()
{
string folder=localFolder + "\\"+DateTime.Now.ToShortDateString();
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
return folder;
}
///<summary>
///在下载结束后清空程序⽬录的多余⽂件
///</summary>
private static void ClearFolder()
{
string folder = Environment.CurrentDirectory;
string[] dictorys = Directory.GetFiles(folder);
foreach (string dictory in dictorys)
{
FileInfo info = new FileInfo(dictory);
if (info.Length == 0)
File.Delete(dictory);
}
}
///<summary>
///递归获取ftp⽂件夹的内容
///</summary>
/
//<param name="fileMark">⽂件标记</param>
///<param name="path">远程路径</param>
///<param name="client"></param>
///<param name="folder"></param>
private static void GetFolder(string fileMark, string path, FTPClient client, string folder)        {
string[] dirs = client.Dir(path);  //获取⽬录下的内容
client.ChDir(path);  //改变⽬录
foreach (string dir in dirs)
{
string[] infos = dir.Split(' ');
string info = infos[infos.Length - 1].Replace("\r", "");
if (dir.StartsWith("d") && !string.IsNullOrEmpty(dir))  //为⽬录
{
if (!info.EndsWith(".") && !info.EndsWith(".."))  //筛选出真实的⽬录
{
Directory.CreateDirectory(folder + "\\" + info);
GetFolder(fileMark, path + "/" + info, client, folder + "\\" + info);
client.ChDir(path);
}
}
else if (dir.StartsWith("-r"))  //为⽂件
{
string file = folder + "\\" + info;
if (File.Exists(file))
{
long remotingSize = client.GetFileSize(info);
FileInfo fileInfo = new FileInfo(file);
long localSize = fileInfo.Length;
if (remotingSize != localSize)  //短点续传
{
client.GetBrokenFile(info, folder, info, localSize);
}
}
else
{
client.GetFile(info, folder, info);  //下载⽂件
Console.WriteLine("⽂件" + folder + info + "已经下载");
}
}
}
}
}
}
配置⽂件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="remotingFolder" value="/temp"/>
<add key="localFolder" value="c:\temp"/>
<add key="ftpServer" value="*"/>
<add key="user" value="*"/>
<add key="pwd" value="*"/>
<add key="port" value="21"/>
</appSettings>
</configuration>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。