[Implement] FTP 上傳/下載 等相關操作
使用 FtpWebRequest 與 FtpWebResponse 執行 FTP 相關操作
需 using 下列
using System.Net;
using System.IO;
{
static void Main(string[] args)
{
//如果 Client 使用 Http Proxy 連至網路,
//並使用 Ftp 相關行為(上傳, 下載, 新增目錄, 刪除目錄) 時,那麼可能會出現:
//使用 HTTP Proxy 時,不支援要求的 FTP 命令。
//要解決此問題,只要將 Proxy 設定為下列參數即可, ftpRequest 為 FtpWebRequest 物件
//ftpRequest.Proxy = GlobalProxySelection.GetEmptyWebProxy();
//或
//ftpRequest.Proxy = null;
//注意的是,URL的最後面不需跟著[ / ],正確的URL : ftp://127.0.0.1 , 錯誤的URL : ftp://127.0.0.1/
Program p = new Program();
//bool result1 = p.FTP_MakeDirectory("ftp://IP:PORT/test/", "abc", "account", "password");
//bool result2 = p.FTP_DeleteDirectory("ftp://IP:PORT/test/", "abc", "account", "password");
bool result3 = p.FTP_Upload("ftp://IP:PORT/test/test.pptx", @"D:\test.pptx", "account", "password");
//bool result4 = p.FTP_Download("ftp://IP:PORT/test/test.pptx", @"D:\123456789.pptx", "account", "password");
//bool result5 = p.FTP_DeleteFile("ftp://IP:PORT/test/test.pptx", "account", "password");
List<string> result6 = p.FTP_GetFileList("ftp://IP:PORT/test/", "account", "password");
}
public bool FTP_Upload(string serverPath, string localPath, string userName, string password)
{
Stream requestStream = null;
FileStream fileStream = null;
FtpWebRequest ftpRequest = null;
FtpWebResponse ftpResponse = null;
bool result = true;
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create(serverPath);
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;//設定Method上傳檔案
ftpRequest.Proxy = null;
if (userName.Length > 0)//如果需要帳號登入
{
NetworkCredential nc = new NetworkCredential(userName, password);
ftpRequest.Credentials = nc; //設定帳號
}
requestStream = ftpRequest.GetRequestStream();
fileStream = File.Open(localPath, FileMode.Open);
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{//開始上傳資料流
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Close();
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
}
catch (Exception ex)
{
//throw new Exception(ex.Message);
result = false;
}
finally
{
if (ftpRequest != null)
{
ftpRequest.Abort();
ftpRequest = null;
}
if (ftpResponse != null)
{
ftpResponse.Close();
ftpResponse = null;
}
if (fileStream != null)
{
fileStream.Close();
fileStream = null;
}
if (requestStream != null)
{
requestStream.Close();
requestStream = null;
}
}
return result;
}
public bool FTP_Download(string serverPath, string localPath, string userName, string Password)
{
Stream responseStream = null;
FileStream fileStream = null;
StreamReader streamReader = null;
FtpWebRequest ftpRequest = null;
FtpWebResponse ftpResponse = null;
bool result = true;
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create(serverPath);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; //設定Method下載檔案
ftpRequest.Proxy = null;
if (userName.Length > 0)//如果需要帳號登入
{
NetworkCredential nc = new NetworkCredential(userName, Password);
ftpRequest.Credentials = nc;//設定帳號
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
responseStream = ftpResponse.GetResponseStream();//取得FTP伺服器回傳的資料流
string fileName = Path.GetFileName(ftpRequest.RequestUri.AbsolutePath);
if (fileName.Length == 0)
{
streamReader = new StreamReader(responseStream);
throw new Exception(streamReader.ReadToEnd());
}
else
{
fileStream = File.Create(localPath);
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{//開始將資料流寫入到本機
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
fileStream.Write(buffer, 0, bytesRead);
}
}
}
catch (IOException ex)
{
//throw new Exception(ex.Message);
result = false;
}
finally
{
if (streamReader != null)
{
streamReader.Close();
streamReader = null;
}
else if (responseStream != null)
{
responseStream.Close();
responseStream = null;
}
if (fileStream != null)
{
fileStream.Close();
fileStream = null;
}
if (ftpRequest != null)
{
ftpRequest.Abort();
ftpRequest = null;
}
if (ftpResponse != null)
{
ftpResponse.Close();
ftpResponse = null;
}
}
return result;
}
public bool FTP_DeleteFile(string serverPath, string userName, string Password)
{
FtpWebRequest ftpRequest = null;
FtpWebResponse ftpResponse = null;
bool result = true;
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create(serverPath);
ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
ftpRequest.Proxy = null;
ftpRequest.UseBinary = true;
ftpRequest.KeepAlive = false;
if (userName.Length > 0)//如果需要帳號登入
{
NetworkCredential nc = new NetworkCredential(userName, Password);
ftpRequest.Credentials = nc;//設定帳號
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
}
catch (IOException ex)
{
//throw new Exception(ex.Message);
result = false;
}
finally
{
if (ftpRequest != null)
{
ftpRequest.Abort();
ftpRequest = null;
}
if (ftpResponse != null)
{
ftpResponse.Close();
ftpResponse = null;
}
}
return result;
}
public bool FTP_MakeDirectory(string targetPath, string createDirName, string userName, string password)
{
FtpWebRequest ftpRequest = null;
FtpWebResponse ftpResponse = null;
bool result = true;
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(targetPath + createDirName);
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
ftpRequest.Proxy = null;
ftpRequest.UseBinary = true;
if (userName.Length > 0)//如果需要帳號登入
{
NetworkCredential nc = new NetworkCredential(userName, password);
ftpRequest.Credentials = nc;//設定帳號
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
}
catch (Exception ex)
{
//throw new Exception(ex.Message);
result = false;
}
finally
{
if (ftpRequest != null)
{
ftpRequest.Abort();
ftpRequest = null;
}
if (ftpResponse != null)
{
ftpResponse.Close();
ftpResponse = null;
}
}
return result;
}
public bool FTP_DeleteDirectory(string targetPath, string deleteDirName, string userName, string password)
{
FtpWebRequest ftpRequest = null;
FtpWebResponse ftpResponse = null;
bool result = true;
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(targetPath + deleteDirName);
ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
ftpRequest.Proxy = null;
ftpRequest.UseBinary = true;
if (userName.Length > 0)//如果需要帳號登入
{
NetworkCredential nc = new NetworkCredential(userName, password);
ftpRequest.Credentials = nc;//設定帳號
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
}
catch (Exception ex)
{
//throw new Exception(ex.Message);
result = true;
}
finally
{
if (ftpRequest != null)
{
ftpRequest.Abort();
ftpRequest = null;
}
if (ftpResponse != null)
{
ftpResponse.Close();
ftpResponse = null;
}
}
return result;
}
public List<string> FTP_GetFileList(string serverPath, string userName, string password)
{
StreamReader streamReader = null;
FtpWebRequest ftpRequest = null;
FtpWebResponse ftpResponse = null;
List<string> result = new List<string>();
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create(serverPath);
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;// //設定Method取得目錄資訊
if (userName.Length > 0)//如果需要帳號登入
{
NetworkCredential nc = new NetworkCredential(userName, password);
ftpRequest.Credentials = nc; //設定帳號
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
streamReader = new StreamReader(ftpResponse.GetResponseStream());
while (streamReader.Peek() >= 0)
{//開始讀取回傳資訊
result.Add(streamReader.ReadLine());
}
}
catch (Exception ex)
{
result.Clear();
result.Add("error");
result.Add(ex.Message);
//throw new Exception(ex.Message);
}
finally
{
if (streamReader != null)
{
streamReader.Close();
streamReader = null;
}
if (ftpRequest != null)
{
ftpRequest.Abort();
ftpRequest = null;
}
if (ftpResponse != null)
{
ftpResponse.Close();
ftpResponse = null;
}
}
return result;//回傳目前清單
}
}
P.S 也可使用 System.Net.WebClient 上傳/下載 FTP,