winscp开启ftp_FTP工具FileZilla、WinSCP、FTP类库FluentFTP

public classFtpFileMetadata

{public long FileLength { get; set; }public string MD5Hash { get; set; }public DateTime LastModifyTime { get; set; }

}public classFtpHelper

{private FtpClient _client = null;private string _host = "127.0.0.1";private int _port = 21;private string _username = "Anonymous";private string _password = "";private string _workingDirectory = "";public stringWorkingDirectory

{get{return_workingDirectory;

}

}public FtpHelper(string host, int port, string username, stringpassword)

{

_host=host;

_port=port;

_username=username;

_password=password;

}public Stream GetStream(stringremotePath)

{

Open();return_client.OpenRead(remotePath);

}public void Get(string localPath, stringremotePath)

{

Open();

_client.DownloadFile(localPath, remotePath,true);

}public void Upload(Stream s, stringremotePath)

{

Open();

_client.Upload(s, remotePath, FtpExists.Overwrite,true);

}public void Upload(string localFile, stringremotePath)

{

Open();using (FileStream fileStream = newFileStream(localFile, FileMode.Open))

{

_client.Upload(fileStream, remotePath, FtpExists.Overwrite,true);

}

}public int UploadFiles(IEnumerable localFiles, stringremoteDir)

{

Open();

List files = new List();foreach (var lf inlocalFiles)

{

files.Add(newFileInfo(lf));

}int count = _client.UploadFiles(files, remoteDir, FtpExists.Overwrite, true, FtpVerify.Retry);returncount;

}public void MkDir(stringdirName)

{

Open();

_client.CreateDirectory(dirName);

}public bool FileExists(stringremotePath)

{

Open();return_client.FileExists(remotePath);

}public bool DirExists(stringremoteDir)

{

Open();return_client.DirectoryExists(remoteDir);

}public FtpListItem[] List(stringremoteDir)

{

Open();var f =_client.GetListing();

FtpListItem[] listItems=_client.GetListing(remoteDir);returnlistItems;

}public FtpFileMetadata Metadata(stringremotePath)

{

Open();long size =_client.GetFileSize(remotePath);

DateTime lastModifyTime=_client.GetModifiedTime(remotePath);return newFtpFileMetadata()

{

FileLength=size,

LastModifyTime=lastModifyTime

};

}public boolTestConnection()

{return_client.IsConnected;

}public void SetWorkingDirectory(stringremoteBaseDir)

{

Open();if (!DirExists(remoteBaseDir))

MkDir(remoteBaseDir);

_client.SetWorkingDirectory(remoteBaseDir);

_workingDirectory=remoteBaseDir;

}private voidOpen()

{if (_client == null)

{

_client= new FtpClient(_host, newSystem.Net.NetworkCredential(_username, _password));

_client.Port= 21;

_client.RetryAttempts= 3;if (!string.IsNullOrWhiteSpace(_workingDirectory))

{

_client.SetWorkingDirectory(_workingDirectory);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值