Renaming file with FtpWebRequest(使用 FtpWebRequest 重命名文件)
问题描述
将文件移动到另一个 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 重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 FtpWebRequest 重命名文件
基础教程推荐
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
