問(wèn)題描述
將文件移動(dòng)到另一個(gè) FTP 位置時(shí),您必須將 RenameTo
與新的 FTP 位置一起使用.
When moving a file to another FTP location you must use RenameTo
with the new FTP location.
在本例中,如何使用 RenameTo
移動(dòng)到新的 FTP 位置?
In this example how do you use RenameTo
to move to the new FTP location?
FtpWebRequest ftpRequest = null;
FtpWebResponse ftpResponse = null;
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://mysite.com/folder1/fileName.ext");
ftpRequest.Credentials = new NetworkCredential("user", "pass");
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.Rename;
ftpRequest.RenameTo = "ftp://mysite.com/folder2/fileName.ext";
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Label1.Text = (ex.ToString()); }
推薦答案
為 .RenameTo
屬性設(shè)置目標(biāo)路徑,而不是 URL:
Set a target path to .RenameTo
property, not URL:
ftpRequest.RenameTo = "/folder2/fileName.ext";
根據(jù) FtpWebRequest 的 MSDN 文檔.RenameTo
屬性:
As per MSDN documentation for FtpWebRequest.RenameTo
property:
獲取或設(shè)置要重命名的文件的新名稱.
Gets or sets the new name of a file being renamed.
比較 WebRequest.Create
方法:
Compare to WebRequest.Create
method:
為指定的 URI 方案初始化一個(gè)新的 WebRequest 實(shí)例.
Initializes a new WebRequest instance for the specified URI scheme.
RenameTo
中的 URL 將是多余的,因?yàn)槟荒苁褂盟鼇?lái)重命名".文件到另一臺(tái)服務(wù)器.
The URL would be redundant in RenameTo
, as you cannot use it to "rename" the file to another server.
這篇關(guān)于使用 FtpWebRequest 重命名文件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!