問題描述
將文件移動到另一個 FTP 位置時,您必須將 RenameTo
與新的 FTP 位置一起使用.
When moving a file to another FTP location you must use RenameTo
with the new FTP location.
在本例中,如何使用 RenameTo
移動到新的 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
屬性設置目標路徑,而不是 URL:
Set a target path to .RenameTo
property, not URL:
ftpRequest.RenameTo = "/folder2/fileName.ext";
根據 FtpWebRequest 的 MSDN 文檔.RenameTo
屬性:
As per MSDN documentation for FtpWebRequest.RenameTo
property:
獲取或設置要重命名的文件的新名稱.
Gets or sets the new name of a file being renamed.
比較 WebRequest.Create
方法:
Compare to WebRequest.Create
method:
為指定的 URI 方案初始化一個新的 WebRequest 實例.
Initializes a new WebRequest instance for the specified URI scheme.
RenameTo
中的 URL 將是多余的,因為您不能使用它來重命名".文件到另一臺服務器.
The URL would be redundant in RenameTo
, as you cannot use it to "rename" the file to another server.
這篇關于使用 FtpWebRequest 重命名文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!