使用 FtpWebRequest 重命名文件 [英] Renaming file with FtpWebRequest

查看:32
本文介绍了使用 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 重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆