这篇文章主要为大家详细介绍了如何利用C#实现一个简单的FTP操作工具,可以实现FTP上传、下载、重命名、刷新、删除功能,感兴趣的可以了解一下
实现功能
实现使用FTP上传、下载、重命名、刷新、删除功能
开发环境
开发工具: Visual Studio 2013
.NET Framework版本:4.5
实现代码
/*FTP操作公共类*/
private string FtpIp, FtpPort, FtpUser, FtpPwd, FtpUrl;
private FTPUtil()
{
}
public FTPUtil(string ftpIp, string ftpPort, string ftpUser, string ftpPwd)
{
FtpIp = ftpIp;
FtpPort = ftpPort;
FtpUser = ftpUser;
FtpPwd = ftpPwd;
FtpUrl = "ftp://" + ftpIp + ":" + ftpPort + "/";
}
private FtpWebRequest GetFtpWebRequest(string path, string method)
{
FtpWebRequest Ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpUrl + "/" + path));
Ftp.Credentials = new NetworkCredential(FtpUser, FtpPwd);
Ftp.KeepAlive = false;
Ftp.UsePassive = true;
Ftp.Method = method;
return Ftp;
}
/// <summary>
/// 获取路径下所有文件夹
/// </summary>
/// <param name="dirName"></param>
/// <returns></returns>
public List<FileModel> GetDirs(string dirName)
{
return GetAllFiles(dirName).FindAll(s => s.Type == "文件夹");
}
/// <summary>
/// 获取路径下所有文件
/// </summary>
/// <param name="dirName"></param>
/// <returns></returns>
public List<FileModel> GetFiles(string dirName)
{
return GetAllFiles(dirName).FindAll(s => s.Type == "文件");
}
/// <summary>
/// 获取路径下所有项目
/// </summary>
/// <param name="dirName"></param>
/// <returns></returns>
public List<FileModel> GetAllFiles(string dirName)
{
List<FileModel> fileList = new List<FileModel>();
try
{
FtpWebRequest Ftp = GetFtpWebRequest(dirName, WebRequestMethods.Ftp.ListDirectoryDetails);
using (WebResponse response = Ftp.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string line = "";
while ((line = reader.ReadLine()) != null)
{
fileList.Add(ConvertFile(line, dirName));
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return fileList;
}
/// <summary>
/// FTP文件信息转换
/// </summary>
/// <param name="value"></param>
/// <param name="dirName"></param>
/// <returns></returns>
private FileModel ConvertFile(string value, string dirName)
{
string[] arr = value.Split(new string[] { " " },4, StringSplitOptions.RemoveEmptyEntries);
FileModel model = new FileModel();
model.Date = arr[0];
model.Time = arr[1];
if (arr[2] == "<DIR>")
{
model.Type = "文件夹";
model.Size = 0;
}
else
{
model.Type = "文件";
model.Size = Convert.ToInt64(arr[2]);
}
model.Name = arr[3];
model.FullName = dirName + "/" + model.Name;
return model;
}
/// <summary>
/// 上传
/// </summary>
/// <param name="fileName"></param>
/// <param name="desFile"></param>
public void Upload(string fileName, string desFile)
{
try
{
FileInfo fileInfo = new FileInfo(fileName);
FtpWebRequest Ftp = GetFtpWebRequest(desFile, WebRequestMethods.Ftp.UploadFile);
Ftp.UseBinary = true;
Ftp.ContentLength = fileInfo.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int len = 0;
using (FileStream fs = fileInfo.OpenRead())
{
using (Stream stream = Ftp.GetRequestStream())
{
while ((len = fs.Read(buff, 0, buffLength)) != 0)
{
stream.Write(buff, 0, buffLength);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 下载
/// </summary>
/// <param name="fileName"></param>
/// <param name="desFile"></param>
public void DownLoad(string fileName, string desFile)
{
try
{
FtpWebRequest Ftp = GetFtpWebRequest(fileName, WebRequestMethods.Ftp.DownloadFile);
Ftp.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)Ftp.GetResponse();
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int len = 0;
using (FileStream fs = new FileStream(desFile, FileMode.Create))
{
using (Stream stream = response.GetResponseStream())
{
while ((len = stream.Read(buff, 0, buffLength)) != 0)
{
fs.Write(buff, 0, buffLength);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="fileName"></param>
public void DeleteFile(string fileName)
{
try
{
FtpWebRequest Ftp = GetFtpWebRequest(fileName, WebRequestMethods.Ftp.DeleteFile);
FtpWebResponse response = (FtpWebResponse)Ftp.GetResponse();
using (Stream datastream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(datastream))
{
sr.ReadToEnd();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 重命名
/// </summary>
/// <param name="fileName"></param>
/// <param name="newName"></param>
public void ReName(string fileName, string newName)
{
try
{
FtpWebRequest Ftp = GetFtpWebRequest(fileName, WebRequestMethods.Ftp.Rename);
Ftp.RenameTo = newName;
Ftp.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)Ftp.GetResponse();
using (Stream datastream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(datastream))
{
sr.ReadToEnd();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
实现效果
FTP 操作工具视频演示
以上就是基于C#实现一个简单的FTP操作工具的详细内容,更多关于C# FTP操作工具的资料请关注得得之家其它相关文章!
沃梦达教程
本文标题为:基于C#实现一个简单的FTP操作工具
基础教程推荐
猜你喜欢
- C# windows语音识别与朗读实例 2023-04-27
- winform把Office转成PDF文件 2023-06-14
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# 调用WebService的方法 2023-03-09
- C#类和结构详解 2023-05-30
- 一个读写csv文件的C#类 2022-11-06
- unity实现动态排行榜 2023-04-27
- C#控制台实现飞行棋小游戏 2023-04-22
- ZooKeeper的安装及部署教程 2023-01-22
- C# List实现行转列的通用方案 2022-11-02