c# - 在 C# 中使用 FTP 移动文件

标签 c# asynchronous ftp

目前我的应用程序的 FTP 上传部分如下所示。我需要做的是,使用 target_file_tmp_name 上传文件,并在上传完成后将其移动(或重命名)到 target_file_name。无法弄清楚如何等待上传完成然后重命名远程文件。

有什么建议吗?

var result_file_name = root_dir + "\\" + file_name + ".eps";
System.Uri target_file_name = new Uri(ftp_path + "/" + file_name + ".eps");
System.Uri target_file_tmp_name = new Uri(ftp_path + "/" + file_name + ".tmp");

await WhenFileCreated(result_file_name);
if (File.Exists(result_file_name))
{
    SetProgressText("Uploading to server...");
    WebClient wc_uploader = new WebClient();

    wc_uploader.Credentials = new NetworkCredential("user", "pass");
    wc_uploader.UploadFileCompleted += FinishProcess;
    wc_uploader.UploadFileAsync(target_file_tmp_name, "STOR", result_file_name);

}

最佳答案

您的FinishProcess 必须类似于以下代码:

 private void FinishProcess(object sender, System.EventArgs e)
        {
            var requestFTP = (FtpWebRequest)FtpWebRequest.Create(target_file_tmp_name);
            requestFTP.Proxy = null;
            requestFTP.Credentials = new NetworkCredential(ftp_login, ftp_pass);
            requestFTP.Method = WebRequestMethods.Ftp.Rename;
            requestFTP.RenameTo = file_name + ".eps";
            requestFTP.GetResponse();
            ...

关于c# - 在 C# 中使用 FTP 移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42339942/

相关文章:

C# & LINQ,一次选择两个(连续的)项目

c# - 如何在MVVM Light和WPF中从父级创建子级窗口?

linux - vim:通过 ftp 替换文件

java - Apache Camel FTP 组件

linux - 通过 FTP 命令或使用 FTP 命令的 shell 脚本上传到 Linux 服务器后的 TAR 大小差异

c# - 从 C# 调用非托管 C++ 代码

c# - out 函数参数的棘手 C# 语法

android - 避免在 Android 应用程序的 Android Volley 中出现 Race condition

javascript - 在事件处理程序中异步执行函数是否有意义?

javascript - Promise.all 替换?