/// <summary> /// 上傳檔案 /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="fileName">檔案名稱(可更改)</param> /// <param name="localFilePath">地端資料夾路徑</param> /// <param name="localFileName">地端檔案名稱</param> /// <returns></returns> public FTPExecuteResult UploadFile(string ftpFolderPath, string fileName, string localFilePath, string localFileName) { try { string ftpPath = Path.Combine(ftpFolderPath, fileName); string localPath = Path.Combine(localFilePath, localFileName); if (File.Exists(localPath)) { using (var fileStream = File.OpenRead(localPath)) { using (var ftpStream = _ftp.OpenWrite(ftpPath)) { var buffer = new byte[8 * 1024]; int count; while ((count = fileStream.Read(buffer, 0, buffer.Length)) > 0) { ftpStream.Write(buffer, 0, count); } } } return(FTPExecuteResult.Ok("上傳成功")); } else { return(FTPExecuteResult.Fail(string.Format("上傳FTP檔案失敗,原因:{0}", "地端無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("上傳FTP檔案失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// FTP上移動資料夾,並修改資料夾名稱 /// </summary> /// <param name="oldFtpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="oldFolderName">原本資料夾名稱</param> /// <param name="newFtpFolderPath">新的資料夾名稱</param> /// <param name="newFolderName">新的資料夾路徑,根目錄請代空字串</param> /// <returns></returns> public FTPExecuteResult MoveFolder(string oldFtpFolderPath, string oldFolderName, string newFtpFolderPath, string newFolderName) { try { if (IsFolderExists(oldFtpFolderPath, oldFolderName)) { if (_ftp.MoveDirectory(Path.Combine(oldFtpFolderPath, oldFolderName), Path.Combine(newFtpFolderPath, newFolderName))) { return(FTPExecuteResult.Ok("異動成功。")); } else { return(FTPExecuteResult.Fail("異動FTP資料夾失敗。")); } } else { return(FTPExecuteResult.Fail(string.Format("異動FTP資料夾失敗,原因:{0}", "FTP上無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("異動FTP資料夾失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 上傳檔案 /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="fileName">檔案名稱(可更改)</param> /// <param name="localFilePath">地端資料夾路徑</param> /// <param name="localFileName">地端檔案名稱</param> /// <returns></returns> public FTPExecuteResult UploadFile(string ftpFolderPath, string fileName, string localFilePath, string localFileName) { try { string ftpPath = Path.Combine(ftpFolderPath, fileName); string localPath = Path.Combine(localFilePath, localFileName); if (File.Exists(localPath)) { using (var fileStream = File.OpenRead(localPath)) _sftp.UploadFile(fileStream, ftpPath); return(FTPExecuteResult.Ok("上傳成功")); } else { return(FTPExecuteResult.Fail(string.Format("上傳SFTP檔案失敗,原因:{0}", "地端無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("上傳SFTP檔案失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 比對Service跟地端資料大小是否一致 /// </summary> /// <param name="ftpFolderPath"></param> /// <param name="fileName"></param> /// <param name="localFilePath"></param> /// <param name="localFileName"></param> /// <returns></returns> public FTPExecuteResult CheckDataConsistent(string ftpFolderPath, string fileName, string localFilePath, string localFileName) { try { long ftpSize = GetFileSize(ftpFolderPath, fileName); string localPath = Path.Combine(localFilePath, localFileName); if (File.Exists(localPath)) { long localSize = new FileInfo(localPath).Length; if (ftpSize == localSize) { return(FTPExecuteResult.Ok("FTP上與地端檔案一致。")); } else { return(FTPExecuteResult.Fail("地端檔案與上傳檔案大小不一致。")); } } else { return(FTPExecuteResult.Fail(string.Format("FTP檔案與地端檔案比較失敗,原因:{0}", "地端無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("FTP檔案與地端檔案比較失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 在FTP上建立資料夾 /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="folderName">資料夾名稱</param> /// <returns></returns> public FTPExecuteResult CreateFolder(string ftpFolderPath, string folderName) { try { if (!IsFolderExists(ftpFolderPath, folderName)) { if (_ftp.CreateDirectory(Path.Combine(ftpFolderPath, folderName))) { return(FTPExecuteResult.Ok("資料夾建立成功。")); } else { return(FTPExecuteResult.Fail("FTP建立資料夾失敗。")); } } else { return(FTPExecuteResult.Fail(string.Format("FTP建立資料夾失敗,原因:{0}", "FTP上資料夾已存在"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("FTP建立資料夾失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 刪除資料夾(不可以刪除檔案) /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="folderName">資料夾名稱</param> /// <returns></returns> public FTPExecuteResult RemoveFolder(string ftpFolderPath, string folderName) { try { string uriPath = string.Format("{0}{1}/{2}/{3}", "FTP://", _ftpServerIP, ftpFolderPath, folderName); if (IsFolderExists(ftpFolderPath, folderName)) { FtpWebRequest ftp = SettingFTP(uriPath); // 關閉/保持 連線 ftp.KeepAlive = false; // 移除資料夾 ftp.Method = WebRequestMethods.Ftp.RemoveDirectory; // 刪除資料夾 FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse(); ftpWebResponse.Close(); return(FTPExecuteResult.Ok("成功刪除資料夾")); } else { return(FTPExecuteResult.Fail(string.Format("FTP刪除資料夾失敗,原因:{0}", "FTP上無此資料夾"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("FTP刪除資料夾失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 下載整個folder的檔案(不包含資料夾) /// </summary> /// <param name="ftpFolderPath"></param> /// <param name="localFilePath"></param> /// <returns></returns> public FTPExecuteResult DownloadFolder(string ftpFolderPath, string localFilePath) { try { var dataList = GetFileFullNameList(ftpFolderPath); foreach (var item in dataList) { string fileName = Path.GetFileName(item); FTPExecuteResult downloadResult = DownloadFile(ftpFolderPath, fileName, localFilePath, fileName); if (!downloadResult.IsSuccessed) { return(downloadResult); } else { FTPExecuteResult checkDataConsistentResult = CheckDataConsistent(ftpFolderPath, fileName, localFilePath, fileName); if (!checkDataConsistentResult.IsSuccessed) { return(checkDataConsistentResult); } } } return(FTPExecuteResult.Ok("下載資料夾檔案成功。")); } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("下載FTP資料夾檔案失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 上傳整個資料夾 /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="localFilePath">地端資料夾路徑</param> /// <returns></returns> public FTPExecuteResult UploadFolder(string ftpFolderPath, string localFilePath) { try { var dataList = Directory.EnumerateFiles(localFilePath); foreach (var item in dataList) { string fileName = Path.GetFileName(item); FTPExecuteResult uploadResult = UploadFile(ftpFolderPath, fileName, localFilePath, fileName); if (!uploadResult.IsSuccessed) { return(uploadResult); } else { FTPExecuteResult checkDataConsistentResult = CheckDataConsistent(ftpFolderPath, fileName, localFilePath, fileName); if (!checkDataConsistentResult.IsSuccessed) { return(checkDataConsistentResult); } } } return(FTPExecuteResult.Ok("上傳資料夾檔案成功。")); } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("FTP上傳資料夾檔案失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// FTP上移動資料夾,並修改資料夾名稱 /// </summary> /// <param name="oldFtpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="oldFolderName">原本資料夾名稱</param> /// <param name="newFtpFolderPath">新的資料夾名稱</param> /// <param name="newFolderName">新的資料夾路徑,根目錄請代空字串</param> /// <returns></returns> public FTPExecuteResult MoveFolder(string oldFtpFolderPath, string oldFolderName, string newFtpFolderPath, string newFolderName) { try { if (IsFolderExists(oldFtpFolderPath, oldFolderName)) { string uriPath = string.Format("{0}{1}/{2}/{3}", "FTP://", _ftpServerIP, oldFtpFolderPath, oldFolderName); FtpWebRequest ftp = SettingFTP(uriPath); ftp.Method = WebRequestMethods.Ftp.Rename; ftp.RenameTo = string.Format("{0}/{1}", newFtpFolderPath, newFolderName); StreamReader sr = new StreamReader(ftp.GetResponse().GetResponseStream(), Encoding.UTF8); sr.Close(); sr.Dispose(); return(FTPExecuteResult.Ok("異動成功。")); } else { return(FTPExecuteResult.Fail(string.Format("異動FTP資料夾失敗,原因:{0}", "FTP上無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("異動FTP資料夾失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 刪除檔案(資料夾不行) /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="fileName">檔案名稱</param> /// <returns></returns> public FTPExecuteResult DeleteFile(string ftpFolderPath, string fileName) { try { string uriPath = string.Format("{0}{1}/{2}/{3}", "FTP://", _ftpServerIP, ftpFolderPath, fileName); if (IsFileExists(ftpFolderPath, fileName)) { FtpWebRequest ftp = SettingFTP(uriPath); // 設定連線模式及相關參數 // 關閉/保持 連線 ftp.KeepAlive = false; // 刪除檔案 ftp.Method = WebRequestMethods.Ftp.DeleteFile; // 刪除檔案 FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse(); ftpWebResponse.Close(); return(FTPExecuteResult.Ok("刪除檔案成功。")); } else { return(FTPExecuteResult.Fail(string.Format("FTP刪除檔案失敗,原因:{0}", "FTP上無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("FTP刪除檔案失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 在FTP上建立資料夾 /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="folderName">資料夾名稱</param> /// <returns></returns> public FTPExecuteResult CreateFolder(string ftpFolderPath, string folderName) { try { if (!IsFolderExists(ftpFolderPath, folderName)) { string uriPath = string.Format("{0}{1}/{2}/{3}", "FTP://", _ftpServerIP, ftpFolderPath, folderName); FtpWebRequest ftp = SettingFTP(uriPath); // 關閉/保持 連線 ftp.KeepAlive = false; // 建立目錄模式 ftp.Method = WebRequestMethods.Ftp.MakeDirectory; // 創建目錄 FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse(); ftpWebResponse.Close(); return(FTPExecuteResult.Ok("資料夾建立成功。")); } else { return(FTPExecuteResult.Fail(string.Format("FTP建立資料夾失敗,原因:{0}", "FTP上資料夾已存在"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("FTP建立資料夾失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 下載檔案 /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="fileName">FTP上檔案名稱</param> /// <param name="localFilePath">地端資料夾路徑</param> /// <param name="localFileName">地端檔案名稱(可更改)</param> /// <returns></returns> public FTPExecuteResult DownloadFile(string ftpFolderPath, string fileName, string localFilePath, string localFileName) { try { if (IsFileExists(ftpFolderPath, fileName)) { string ftpPath = Path.Combine(ftpFolderPath, fileName); string localPath = Path.Combine(localFilePath, localFileName); using (Stream ftpStream = _ftp.OpenRead(ftpPath)) { if (ftpStream.Length != 0) { using (FileStream fileStream = File.Create(localPath, (int)ftpStream.Length)) { var buffer = new byte[200 * 1024]; int count; while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0) { fileStream.Write(buffer, 0, count); } } } else { using (FileStream fs = File.Create(localPath)) { } }; } return(FTPExecuteResult.Ok("檔案下載成功。")); } else { return(FTPExecuteResult.Fail(string.Format("下載FTP檔案失敗,原因:{0}", "FTP上無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("下載FTP檔案失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 刪除資料夾(不可以刪除檔案) /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="folderName">資料夾名稱</param> /// <returns></returns> public FTPExecuteResult RemoveFolder(string ftpFolderPath, string folderName) { try { if (IsFolderExists(ftpFolderPath, folderName)) { _ftp.DeleteDirectory(Path.Combine(ftpFolderPath, folderName)); return(FTPExecuteResult.Ok("成功刪除資料夾")); } else { return(FTPExecuteResult.Fail(string.Format("FTP刪除資料夾失敗,原因:{0}", "FTP上無此資料夾"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("FTP刪除資料夾失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 刪除檔案(資料夾不行) /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="fileName">檔案名稱</param> /// <returns></returns> public FTPExecuteResult DeleteFile(string ftpFolderPath, string fileName) { try { if (IsFileExists(ftpFolderPath, fileName)) { _ftp.DeleteFile(Path.Combine(ftpFolderPath, fileName)); return(FTPExecuteResult.Ok("刪除檔案成功。")); } else { return(FTPExecuteResult.Fail(string.Format("FTP刪除檔案失敗,原因:{0}", "FTP上無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("FTP刪除檔案失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// SFTP上移動檔案,並修改檔案名稱 /// </summary> /// <param name="oldFtpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="oldFileName">原本檔案名稱(含附檔名)</param> /// <param name="newFtpFolderPath">新的檔案名稱(含附檔名)</param> /// <param name="newFileName">新的資料夾路徑,根目錄請代空字串</param> /// <returns></returns> public FTPExecuteResult MoveFile(string oldFtpFolderPath, string oldFileName, string newFtpFolderPath, string newFileName) { try { if (IsFileExists(oldFtpFolderPath, oldFileName)) { _sftp.RenameFile(string.Format("/{0}/{1}", oldFtpFolderPath, oldFileName), string.Format("/{0}/{1}", newFtpFolderPath, newFileName)); return(FTPExecuteResult.Ok("異動成功。")); } else { return(FTPExecuteResult.Fail(string.Format("異動SFTP檔案失敗,原因:{0}", "SFTP上無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("異動SFTP檔案失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 下載檔案 /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="fileName">FTP上檔案名稱</param> /// <param name="localFilePath">地端資料夾路徑</param> /// <param name="localFileName">地端檔案名稱(可更改)</param> /// <returns></returns> public FTPExecuteResult DownloadFile(string ftpFolderPath, string fileName, string localFilePath, string localFileName) { try { if (IsFileExists(ftpFolderPath, fileName)) { string serverPath = Path.Combine(ftpFolderPath, fileName); string localPath = Path.Combine(localFilePath, localFileName); var byt = _sftp.ReadAllBytes(serverPath); File.WriteAllBytes(localPath, byt); return(FTPExecuteResult.Ok("檔案下載成功。")); } else { return(FTPExecuteResult.Fail(string.Format("下載SFTP檔案失敗,原因:{0}", "SFTP上無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("下載SFTP檔案失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 下載檔案 /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="fileName">FTP上檔案名稱</param> /// <param name="localFilePath">地端資料夾路徑</param> /// <param name="localFileName">地端檔案名稱(可更改)</param> /// <returns></returns> public FTPExecuteResult DownloadFile(string ftpFolderPath, string fileName, string localFilePath, string localFileName) { try { string uriPath = string.Format("{0}{1}/{2}/{3}", "FTP://", _ftpServerIP, ftpFolderPath, fileName); string localPath = Path.Combine(localFilePath, localFileName); if (IsFileExists(ftpFolderPath, fileName)) { FtpWebRequest ftp = SettingFTP(uriPath); // 設定連線模式及相關參數 // FTPS用設定 if (_ftpMode == FTPMode.Explicit) { ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy; ftp.EnableSsl = true; } // 通訊埠接聽並等待連接 ftp.UsePassive = false; // 下傳檔案 ftp.Method = WebRequestMethods.Ftp.DownloadFile; // 取得FTP請求回應 FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse(); // 檔案設為寫入模式 FileStream fileStream = new FileStream(localPath, FileMode.Create, FileAccess.Write); // 資料串流設為上傳至FTP Stream stream = ftpWebResponse.GetResponseStream(); //傳輸位元初始化 byte[] byteBuffer = new byte[2047]; int iRead = 0; do { iRead = stream.Read(byteBuffer, 0, byteBuffer.Length); //接收資料串流 fileStream.Write(byteBuffer, 0, iRead); //寫入下載檔案 //Console.WriteLine("bBuffer: {0} Byte", iRead); } while (!(iRead == 0)); stream.Flush(); stream.Close(); stream.Dispose(); fileStream.Flush(); fileStream.Close(); fileStream.Dispose(); ftpWebResponse.Close(); return(FTPExecuteResult.Ok("檔案下載成功。")); } else { return(FTPExecuteResult.Fail(string.Format("下載FTP檔案失敗,原因:{0}", "FTP上無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("下載FTP檔案失敗,原因:{0}", ex.ToString()))); } }
/// <summary> /// 上傳檔案 /// </summary> /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param> /// <param name="fileName">檔案名稱(可更改)</param> /// <param name="localFilePath">地端資料夾路徑</param> /// <param name="localFileName">地端檔案名稱</param> /// <returns></returns> public FTPExecuteResult UploadFile(string ftpFolderPath, string fileName, string localFilePath, string localFileName) { try { string uriPath = string.Format("{0}{1}/{2}/{3}", "FTP://", _ftpServerIP, ftpFolderPath, fileName); string localPath = Path.Combine(localFilePath, localFileName); if (File.Exists(localPath)) { FtpWebRequest ftp = SettingFTP(uriPath); // 設定連線模式及相關參數 // FTPS用設定 if (_ftpMode == FTPMode.Explicit) { ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy; ftp.EnableSsl = true; } // 關閉/保持 連線 ftp.KeepAlive = false; // 通訊埠接聽並等待連接 ftp.UsePassive = false; //下傳檔案 ftp.Method = WebRequestMethods.Ftp.UploadFile; /* proxy setting (不使用proxy) */ ftp.Proxy = GlobalProxySelection.GetEmptyWebProxy(); ftp.Proxy = null; // 上傳檔案 檔案設為讀取模式 FileStream fileStream = new FileStream(localPath, FileMode.Open, FileAccess.Read); // 資料串流設為上傳至FTP Stream stream = ftp.GetRequestStream(); //傳輸位元初始化 byte[] byteBuffer = new byte[2047]; int iRead = 0; do { // 讀取上傳檔案 iRead = fileStream.Read(byteBuffer, 0, byteBuffer.Length); // 傳送資料串流 stream.Write(byteBuffer, 0, iRead); } while (!(iRead == 0)); fileStream.Flush(); fileStream.Close(); fileStream.Dispose(); stream.Flush(); stream.Close(); stream.Dispose(); return(FTPExecuteResult.Ok("上傳成功")); } else { return(FTPExecuteResult.Fail(string.Format("上傳FTP檔案失敗,原因:{0}", "地端無此檔案"))); } } catch (Exception ex) { return(FTPExecuteResult.Fail(string.Format("上傳FTP檔案失敗,原因:{0}", ex.ToString()))); } }