Skip to main content
 首页 » 编程设计

c#之FTP之以不同的名称上传文件

2024年02月23日13TianFang

我有一个带有单选按钮的表单,每个单选按钮都在一个字符串中给出一个文件名,我想做的是将该字符串作为用户上传的任何文件的名称。

如果你能向我解释如何重命名,那就太好了,因为我已经得到了上传的代码或者只是帮我修改这个功能,我可能不得不添加到参数“字符串类型”:

public void uploadFTP(string filename, string type, string password, ProgressBar progressbar) 
{ 
    WebClient client = new WebClient(); 
    client.Credentials = new System.Net.NetworkCredential("username", password); 
 
    try 
    { 
        client.UploadFileAsync(new Uri(@"ftp://ftpaddress.com" + "/" + new FileInfo(filename).Name), "STOR", filename); 
    } 
    catch(System.InvalidCastException) 
    { 
        // Handled it 
    } 
    catch (System.ArgumentException) 
    { 
        // Handled it 
    } 
    client.UploadProgressChanged += (s, e) => 
    { 
        progressbar.Value = e.ProgressPercentage; 
    }; 
    client.UploadFileCompleted += (s, e) => 
    { 
        MessageBox.Show("Upload complete", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); 
    }; 
} 

如果重要:文件是 RTF (RichTextBox)。

谢谢!

请您参考如下方法:

然后只需上传到不同的 URL。将代码中的 new FileInfo(filename).Name 替换为您实际需要的名称。另外,我认为不使用字符串操作更好。 STOR 命令是默认命令。

client.UploadFileAsync(new Uri(new Uri("ftp://ftpaddress.com"), newName)), filename);