基于C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單的FTP操作工具
實(shí)現(xiàn)功能
實(shí)現(xiàn)使用FTP上傳、下載、重命名、刷新、刪除功能
開(kāi)發(fā)環(huán)境
開(kāi)發(fā)工具: Visual Studio 2013
.NET Framework版本:4.5
實(shí)現(xiàn)代碼
/*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>
/// 獲取路徑下所有項(xiàng)目
/// </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文件信息轉(zhuǎn)換
/// </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;
}
}實(shí)現(xiàn)效果
以上就是基于C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單的FTP操作工具的詳細(xì)內(nèi)容,更多關(guān)于C# FTP操作工具的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# OpenCvSharp利用白平衡技術(shù)實(shí)現(xiàn)圖像修復(fù)功能
這篇文章主要為大家詳細(xì)介紹了C# OpenCvSharp如何利用白平衡技術(shù)實(shí)現(xiàn)圖像修復(fù)功能,文中的示例代碼講解詳細(xì),希望對(duì)大家有一定的幫助2024-02-02
C#中Hashtable和Dictionary的區(qū)別
Hashtable 和 Dictionary 都是 C# 中用于存儲(chǔ)鍵值對(duì)的數(shù)據(jù)結(jié)構(gòu),本文主要介紹了C#中Hashtable和Dictionary的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
Unity Shader實(shí)現(xiàn)新手引導(dǎo)遮罩鏤空效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)新手引導(dǎo)遮罩鏤空效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
C#中實(shí)現(xiàn)Fluent Interface的三種方法
這篇文章主要介紹了C#中實(shí)現(xiàn)Fluent Interface的三種方法,本文講解了Fluent Interface的簡(jiǎn)單實(shí)現(xiàn)、使用裝飾器模式和擴(kuò)展方法實(shí)現(xiàn)Fluent Interface等3種實(shí)現(xiàn)方法,需要的朋友可以參考下2015-03-03
C#實(shí)現(xiàn)Bitmap類型與Byte[]類型相互轉(zhuǎn)化的示例詳解
在C#編程中,Bitmap類型和Byte[]類型之間的相互轉(zhuǎn)化是圖像處理和數(shù)據(jù)傳輸中常見(jiàn)的需求,Bitmap類型表示一個(gè)位圖圖像,而B(niǎo)yte[]類型則是一個(gè)字節(jié)數(shù)組,本文將詳細(xì)介紹如何在這兩種類型之間進(jìn)行相互轉(zhuǎn)化,需要的朋友可以參考下2024-07-07

