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 重命名文件


基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01