在开发项目的时候,需要移动FTP上的文件。网上找到MoveTo方法,并没有实际效果。踩了坑,网上找了很久也没有效果。那是因为我们的文件来源路径和模板路径不正确,需要转为相对路径才能使用。Uri.MakeRelativeUri方法生成一个文件相对路径,代码如下:
/// <summary>
/// ftp服务器移动文件
/// </summary>
/// <param name="url">文件夹路径</param>
/// <param name="fileName">文件名称</param>
/// <param name="backupUrl">备份路径</param>
/// <param name="newFileName"></param>
public bool MoveFtpFile(string url, string fileName, string backupUrl, string newFileName)
{
//登入到ftp的账号
string ftpUserName = att.ftpUserName;
//登入到ftp的密码
string ftpUserPwd = att.ftpUserPwd;
bool success = false;
var uri = url + backupUrl;
if (!Directory.Exists(uri))
{
FtpWebRequest fre