在C#中使用FTP移动文件

时间:2017-02-20 08:47:36

标签: 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);

}

1 个答案:

答案 0 :(得分:2)

您的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();
            ...