FTP的完整類<3><轉>
#region 重新命名檔案
///
/// 更改一個檔案的名稱或一個目錄的名稱
///
/// 原始檔案或目錄名稱
/// 新的檔案或目錄的名稱
public bool ReName(string RemoteFileName, string NewFileName)
{
try
{
if (!IsValidFileChars(RemoteFileName) || !IsValidFileChars(NewFileName))
{
throw new Exception("檔名非法!");
}
if (RemoteFileName == NewFileName)
{
return true;
}
if (FileExist(RemoteFileName))
{
Request = OpenRequest(new Uri(this.Uri.ToString() + RemoteFileName), WebRequestMethods.Ftp.Rename);
Request.RenameTo = NewFileName;
Response = (FtpWebResponse)Request.GetResponse();
///
/// 更改一個檔案的名稱或一個目錄的名稱
///
/// 原始檔案或目錄名稱
/// 新的檔案或目錄的名稱
public bool ReName(string RemoteFileName, string NewFileName)
{
try
{
if (!IsValidFileChars(RemoteFileName) || !IsValidFileChars(NewFileName))
{
throw new Exception("檔名非法!");
}
if (RemoteFileName == NewFileName)
{
return true;
}
if (FileExist(RemoteFileName))
{
Request = OpenRequest(new Uri(this.Uri.ToString() + RemoteFileName), WebRequestMethods.Ftp.Rename);
Request.RenameTo = NewFileName;
Response = (FtpWebResponse)Request.GetResponse();
}
else
{
throw new Exception("檔案在伺服器上不存在!");
}
return true;
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 複製、移動檔案
///
/// 把當前目錄下面的一個檔案複製到伺服器上面另外的目錄中,注意,複製檔案之後,當前工作目錄還是檔案原來所在的目錄
///
/// 當前目錄下的檔名
/// 新目錄名稱。
/// 說明:如果新目錄是當前目錄的子目錄,則直接指定子目錄。如: SubDirectory1/SubDirectory2 ;
/// 如果新目錄不是當前目錄的子目錄,則必須從根目錄一級一級的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2
///
///
public bool CopyFileToAnotherDirectory(string RemoteFile, string DirectoryName)
{
string CurrentWorkDir = this.DirectoryPath;
try
{
byte[] bt = DownloadFile(RemoteFile);
GotoDirectory(DirectoryName);
bool Success = UploadFile(bt, RemoteFile, false);
this.DirectoryPath = CurrentWorkDir;
return Success;
}
catch (Exception ep)
{
this.DirectoryPath = CurrentWorkDir;
ErrorMsg = ep.ToString();
throw ep;
}
}
///
/// 把當前目錄下面的一個檔案移動到伺服器上面另外的目錄中,注意,移動檔案之後,當前工作目錄還是檔案原來所在的目錄
///
/// 當前目錄下的檔名
/// 新目錄名稱。
/// 說明:如果新目錄是當前目錄的子目錄,則直接指定子目錄。如: SubDirectory1/SubDirectory2 ;
/// 如果新目錄不是當前目錄的子目錄,則必須從根目錄一級一級的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2
///
///
public bool MoveFileToAnotherDirectory(string RemoteFile, string DirectoryName)
{
string CurrentWorkDir = this.DirectoryPath;
try
{
if (DirectoryName == "")
return false;
if (!DirectoryName.StartsWith("/"))
DirectoryName = "/" + DirectoryName;
if (!DirectoryName.EndsWith("/"))
DirectoryName += "/";
bool Success = ReName(RemoteFile, DirectoryName + RemoteFile);
this.DirectoryPath = CurrentWorkDir;
return Success;
}
catch (Exception ep)
{
this.DirectoryPath = CurrentWorkDir;
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 建立、刪除子目錄
///
/// 在FTP伺服器上當前工作目錄建立一個子目錄
///
/// 子目錄名稱
public bool MakeDirectory(string DirectoryName)
{
try
{
if (!IsValidPathChars(DirectoryName))
{
throw new Exception("目錄名非法!");
}
if (DirectoryExist(DirectoryName))
{
throw new Exception("伺服器上面已經存在同名的檔名或目錄名!");
}
Response = Open(new Uri(this.Uri.ToString() + DirectoryName), WebRequestMethods.Ftp.MakeDirectory);
return true;
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
///
/// 從當前工作目錄中刪除一個子目錄
///
/// 子目錄名稱
public bool RemoveDirectory(string DirectoryName)
{
try
{
if (!IsValidPathChars(DirectoryName))
{
throw new Exception("目錄名非法!");
}
if (!DirectoryExist(DirectoryName))
{
throw new Exception("伺服器上面不存在指定的檔名或目錄名!");
}
Response = Open(new Uri(this.Uri.ToString() + DirectoryName), WebRequestMethods.Ftp.RemoveDirectory);
return true;
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 檔案、目錄名稱有效性判斷
///
/// 判斷目錄名中字元是否合法
///
/// 目錄名稱
public bool IsValidPathChars(string DirectoryName)
{
char[] invalidPathChars = Path.GetInvalidPathChars();
char[] DirChar = DirectoryName.ToCharArray();
foreach (char C in DirChar)
{
if (Array.BinarySearch(invalidPathChars, C) >= 0)
{
return false;
}
}
return true;
}
///
/// 判斷檔名中字元是否合法
///
/// 檔名稱
public bool IsValidFileChars(string FileName)
{
char[] invalidFileChars = Path.GetInvalidFileNameChars();
char[] NameChar = FileName.ToCharArray();
foreach (char C in NameChar)
{
if (Array.BinarySearch(invalidFileChars, C) >= 0)
{
return false;
}
}
return true;
}
#endregion
#region 目錄切換操作
///
/// 進入一個目錄
///
///
/// 新目錄的名字。
/// 說明:如果新目錄是當前目錄的子目錄,則直接指定子目錄。如: SubDirectory1/SubDirectory2 ;
/// 如果新目錄不是當前目錄的子目錄,則必須從根目錄一級一級的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2
///
public bool GotoDirectory(string DirectoryName)
{
string CurrentWorkPath = this.DirectoryPath;
try
{
DirectoryName = DirectoryName.Replace("\\", "/");
string[] DirectoryNames = DirectoryName.Split(new char[] { '/' });
if (DirectoryNames[0] == ".")
{
this.DirectoryPath = "/";
if (DirectoryNames.Length == 1)
{
return true;
}
Array.Clear(DirectoryNames, 0, 1);
}
bool Success = false;
foreach (string dir in DirectoryNames)
{
if (dir != null)
{
Success = EnterOneSubDirectory(dir);
if (!Success)
{
this.DirectoryPath = CurrentWorkPath;
return false;
}
}
}
return Success;
else
{
throw new Exception("檔案在伺服器上不存在!");
}
return true;
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 複製、移動檔案
///
/// 把當前目錄下面的一個檔案複製到伺服器上面另外的目錄中,注意,複製檔案之後,當前工作目錄還是檔案原來所在的目錄
///
/// 當前目錄下的檔名
/// 新目錄名稱。
/// 說明:如果新目錄是當前目錄的子目錄,則直接指定子目錄。如: SubDirectory1/SubDirectory2 ;
/// 如果新目錄不是當前目錄的子目錄,則必須從根目錄一級一級的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2
///
///
public bool CopyFileToAnotherDirectory(string RemoteFile, string DirectoryName)
{
string CurrentWorkDir = this.DirectoryPath;
try
{
byte[] bt = DownloadFile(RemoteFile);
GotoDirectory(DirectoryName);
bool Success = UploadFile(bt, RemoteFile, false);
this.DirectoryPath = CurrentWorkDir;
return Success;
}
catch (Exception ep)
{
this.DirectoryPath = CurrentWorkDir;
ErrorMsg = ep.ToString();
throw ep;
}
}
///
/// 把當前目錄下面的一個檔案移動到伺服器上面另外的目錄中,注意,移動檔案之後,當前工作目錄還是檔案原來所在的目錄
///
/// 當前目錄下的檔名
/// 新目錄名稱。
/// 說明:如果新目錄是當前目錄的子目錄,則直接指定子目錄。如: SubDirectory1/SubDirectory2 ;
/// 如果新目錄不是當前目錄的子目錄,則必須從根目錄一級一級的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2
///
///
public bool MoveFileToAnotherDirectory(string RemoteFile, string DirectoryName)
{
string CurrentWorkDir = this.DirectoryPath;
try
{
if (DirectoryName == "")
return false;
if (!DirectoryName.StartsWith("/"))
DirectoryName = "/" + DirectoryName;
if (!DirectoryName.EndsWith("/"))
DirectoryName += "/";
bool Success = ReName(RemoteFile, DirectoryName + RemoteFile);
this.DirectoryPath = CurrentWorkDir;
return Success;
}
catch (Exception ep)
{
this.DirectoryPath = CurrentWorkDir;
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 建立、刪除子目錄
///
/// 在FTP伺服器上當前工作目錄建立一個子目錄
///
/// 子目錄名稱
public bool MakeDirectory(string DirectoryName)
{
try
{
if (!IsValidPathChars(DirectoryName))
{
throw new Exception("目錄名非法!");
}
if (DirectoryExist(DirectoryName))
{
throw new Exception("伺服器上面已經存在同名的檔名或目錄名!");
}
Response = Open(new Uri(this.Uri.ToString() + DirectoryName), WebRequestMethods.Ftp.MakeDirectory);
return true;
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
///
/// 從當前工作目錄中刪除一個子目錄
///
/// 子目錄名稱
public bool RemoveDirectory(string DirectoryName)
{
try
{
if (!IsValidPathChars(DirectoryName))
{
throw new Exception("目錄名非法!");
}
if (!DirectoryExist(DirectoryName))
{
throw new Exception("伺服器上面不存在指定的檔名或目錄名!");
}
Response = Open(new Uri(this.Uri.ToString() + DirectoryName), WebRequestMethods.Ftp.RemoveDirectory);
return true;
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 檔案、目錄名稱有效性判斷
///
/// 判斷目錄名中字元是否合法
///
/// 目錄名稱
public bool IsValidPathChars(string DirectoryName)
{
char[] invalidPathChars = Path.GetInvalidPathChars();
char[] DirChar = DirectoryName.ToCharArray();
foreach (char C in DirChar)
{
if (Array.BinarySearch(invalidPathChars, C) >= 0)
{
return false;
}
}
return true;
}
///
/// 判斷檔名中字元是否合法
///
/// 檔名稱
public bool IsValidFileChars(string FileName)
{
char[] invalidFileChars = Path.GetInvalidFileNameChars();
char[] NameChar = FileName.ToCharArray();
foreach (char C in NameChar)
{
if (Array.BinarySearch(invalidFileChars, C) >= 0)
{
return false;
}
}
return true;
}
#endregion
#region 目錄切換操作
///
/// 進入一個目錄
///
///
/// 新目錄的名字。
/// 說明:如果新目錄是當前目錄的子目錄,則直接指定子目錄。如: SubDirectory1/SubDirectory2 ;
/// 如果新目錄不是當前目錄的子目錄,則必須從根目錄一級一級的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2
///
public bool GotoDirectory(string DirectoryName)
{
string CurrentWorkPath = this.DirectoryPath;
try
{
DirectoryName = DirectoryName.Replace("\\", "/");
string[] DirectoryNames = DirectoryName.Split(new char[] { '/' });
if (DirectoryNames[0] == ".")
{
this.DirectoryPath = "/";
if (DirectoryNames.Length == 1)
{
return true;
}
Array.Clear(DirectoryNames, 0, 1);
}
bool Success = false;
foreach (string dir in DirectoryNames)
{
if (dir != null)
{
Success = EnterOneSubDirectory(dir);
if (!Success)
{
this.DirectoryPath = CurrentWorkPath;
return false;
}
}
}
return Success;
}
catch (Exception ep)
{
this.DirectoryPath = CurrentWorkPath;
ErrorMsg = ep.ToString();
throw ep;
}
}
///
/// 從當前工作目錄進入一個子目錄
///
/// 子目錄名稱
private bool EnterOneSubDirectory(string DirectoryName)
{
try
{
if (DirectoryName.IndexOf("/") >= 0 || !IsValidPathChars(DirectoryName))
{
throw new Exception("目錄名非法!");
}
if (DirectoryName.Length > 0 && DirectoryExist(DirectoryName))
{
if (!DirectoryName.EndsWith("/"))
{
DirectoryName += "/";
}
_DirectoryPath += DirectoryName;
return true;
}
else
{
return false;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
///
/// 從當前工作目錄往上一級目錄
///
public bool ComeoutDirectory()
{
if (_DirectoryPath == "/")
{
ErrorMsg = "當前目錄已經是根目錄!";
throw new Exception("當前目錄已經是根目錄!");
}
char[] sp = new char[1] { '/' };
catch (Exception ep)
{
this.DirectoryPath = CurrentWorkPath;
ErrorMsg = ep.ToString();
throw ep;
}
}
///
/// 從當前工作目錄進入一個子目錄
///
/// 子目錄名稱
private bool EnterOneSubDirectory(string DirectoryName)
{
try
{
if (DirectoryName.IndexOf("/") >= 0 || !IsValidPathChars(DirectoryName))
{
throw new Exception("目錄名非法!");
}
if (DirectoryName.Length > 0 && DirectoryExist(DirectoryName))
{
if (!DirectoryName.EndsWith("/"))
{
DirectoryName += "/";
}
_DirectoryPath += DirectoryName;
return true;
}
else
{
return false;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
///
/// 從當前工作目錄往上一級目錄
///
public bool ComeoutDirectory()
{
if (_DirectoryPath == "/")
{
ErrorMsg = "當前目錄已經是根目錄!";
throw new Exception("當前目錄已經是根目錄!");
}
char[] sp = new char[1] { '/' };
string[] strDir = _DirectoryPath.Split(sp, StringSplitOptions.RemoveEmptyEntries);
if (strDir.Length == 1)
{
_DirectoryPath = "/";
}
else
{
_DirectoryPath = String.Join("/", strDir, 0, strDir.Length - 1);
}
return true;
if (strDir.Length == 1)
{
_DirectoryPath = "/";
}
else
{
_DirectoryPath = String.Join("/", strDir, 0, strDir.Length - 1);
}
return true;
}
#endregion
#region 過載WebClient,支援FTP進度
internal class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
req.UsePassive = false;
return req;
}
}
#endregion
#endregion
#region 過載WebClient,支援FTP進度
internal class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
req.UsePassive = false;
return req;
}
}
#endregion
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22392018/viewspace-712400/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- FTP工具類FTP
- java ftp工具類,帶你領略5款不同的java ftp工具類JavaFTP
- 推薦一個java操作ftp的工具類JavaFTP
- Win10開啟FTP與配置(完整無錯版)Win10FTP
- 一個完整的COM通訊類
- 北大FTP,除了北大FTP其他好用的FTP軟體FTP
- 理解數字化轉型:3個階段、2個分類和3類價值
- 類轉json的基類實現JSON
- 企業網盤替換FTP的3大優勢FTP
- FTP,FTP連線的辦法,配置方式FTP
- 非完整資料聚類初探聚類
- 【轉】Object類的常用方法Object
- 免費的FTP工具,免費的FTP工具下載!FTP
- FTP伺服器搭建踩坑和工具類實現FTP伺服器
- ftp的passive模式FTP模式
- ftp工具,6款好用的ftp工具,來自運維人員好用的ftp工具推薦。FTP運維
- 常用的Java類方法【轉載】Java
- ftp,ftp是幹什麼的,怎麼運用呢?FTP
- 簡單實用的FTP客戶端:Viper FTP for MacFTP客戶端Mac
- FTP的配置和管理FTP
- 公司類別(轉)
- 從 "垃圾 "資料到資料完整性的轉變
- PHP外掛系統的實現(六):完整經理類PHP
- 實用的FTP客戶端:FTP Disk for Mac中文版FTP客戶端Mac
- 簡單好用的ftp檔案傳輸工具:Viper FTP for MacFTPMac
- ftp服務實現本地和s3儲存FTPS3
- 進位制轉換的工具類
- ftp,安全ftp軟體哪個好FTP
- 國內好用的ftp軟體,三款國內好用的ftp軟體FTP
- centos ftpCentOSFTP
- 如何理解Python3中的子類和父類?Python
- FTP的安裝和使用FTP
- FTP 命令的使用詳解FTP
- centos7環境 nginx轉發ftp請求(親測可用)CentOSNginxFTP
- YOLOv3 的 TensorFlow 實現,GitHub 完整原始碼解析YOLOGithub原始碼
- FTP客戶端工具Yummy FTP Pro MacFTP客戶端Mac
- wing ftp server(ftp伺服器軟體)FTPServer伺服器
- unicode轉碼工具類Unicode
- 可平滑替代FTP的FTP替代解決方案,具有哪些強大功能?FTP