c# - 使用 FtpWebRequest 重命名文件

标签 c# .net ftp ftpwebrequest

将文件移动到另一个 FTP 位置时,您必须使用 RenameTo 和新的 FTP 位置。

在此示例中,您如何使用 RenameTo 移动到新的 FTP 位置?

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:

ftpRequest.RenameTo = "/folder2/fileName.ext";
        

根据 FtpWebRequest.RenameTo property 的 MSDN 文档:

Gets or sets the new name of a file being renamed.

WebRequest.Create method 比较:

Initializes a new WebRequest instance for the specified URI scheme.


RenameTo 中的 URL 是多余的,因为您不能使用它来将文件“重命名”到另一台服务器。

关于c# - 使用 FtpWebRequest 重命名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35638532/

相关文章:

c# - 在 C# 中处理 NullReferenceExceptions 的更好方法

c# - VS Code 无法在 Ubuntu 中找到 .Net 6 - 无法编译和运行应用程序

c# - 格式化复选框?

C# Absolute URI 从 URL 中删除 ".."

c - 从 FTP 服务器上的文件获取特定数据 block

c# - 如何从 Windows Phone 应用程序启动 IE7?

c# - system.windows.forms.sendkeys.sendwait 在窗口最大化时不起作用

c# - 如何将 24 位图像转换为 16 位 rgb565 位图?

html - 从 FTP 检索图像到网页

c# - 按模型和计数分页 webgrid