FtpWebRequest類別沒有提供移動檔案的命令,但是可以透過WebRequestMethods.Ftp.Rename達成相同效果的功能。
FtpWebRequest ftp_Request = null/* TODO Change to default(_) if this is not a reference type */;
FtpWebResponse ftp_Response = null/* TODO Change to default(_) if this is not a reference type */;
string str_FtpAct = "XXX";
string str_FtpPwd = "XXX";
// Try to create backup folder. If folder already created, resume to backup.
try
{
ftp_Request = (FtpWebRequest)WebRequest.Create("D:/testfolder/backup");
ftp_Request.Credentials = new NetworkCredential(str_FtpAct, str_FtpPwd);
ftp_Request.Method = WebRequestMethods.Ftp.MakeDirectory;
ftp_Response = ftp_Request.GetResponse();
}
catch (WebException ex_Web)
{
FtpWebResponse ftp_ResponseEx = ex_Web.Response;
int int_ErrCode = ftp_ResponseEx.StatusCode;
// 550: Access is denied, means directory already exist
if (int_ErrCode != 550)
Console.WriteLine("Create Folder Failed!");
}
try
{
ftp_Request = (FtpWebRequest)WebRequest.Create("D:/testfolder/test.txt");
ftp_Request.Credentials = new NetworkCredential(str_FtpAct, str_FtpPwd);
ftp_Request.Method = WebRequestMethods.Ftp.Rename;
ftp_Request.RenameTo = "../testfolder/backup/test.txt";
ftp_Response = (FtpWebResponse)ftp_Request.GetResponse();
}
catch (WebException webex)
{
// Get FTP Error code and exception status
FtpWebResponse ftp_ResponseEx = webex.Response;
int int_FtpCode = ftp_ResponseEx.StatusCode;
// Do something
}
finally
{
if (ftp_Response != null)
ftp_Response.Close();
}
高級水冷氣, 2020/8/4 下午 04:02:46