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


- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01