Move file to parent folder on FTP

2019-05-18 18:07发布

问题:

I'm trying to move files from a folder into it's parent folder.

I had issues moving files before, something to do with absolute versus relative path on the RenameTo property. I'm currently getting a 553 error(file name not allowed).

Files are in "//blah/John/Update/Done/" and I'd like to move to "//../Update/".

Here is a snippet of the code I'm using:

string ftpConn="ftp://blah/John/Update/";
for (int i = 0; i < fileList.Count; i++ )
{
    var requestMove = (FtpWebRequest)WebRequest.Create(ftpConn + "Done/" + fileList[i].fName);
    requestMove.Method = WebRequestMethods.Ftp.Rename;
    requestMove.Credentials = new NetworkCredential(ftpUser, ftpPass);                   
    requestMove.RenameTo = ".../John/Update/" + fileList[i].fName;
    requestMove.GetResponse();
}

I've tried changing the RenameTo property to the absolute path and it still gives me the same error.

回答1:

I don't think ... is valid in relative paths. You probably meant:

requestMove.RenameTo = "./../" + fileList[i].fName;
//                      ^  ^
//        Current dir ──┘  │
//                         │
//      Go up one folder ──┘

If your current working directory is /blah/John/Update/Done/, ./../ effectively represents /blah/John/Update.

More about relative path syntax here.



标签: c# ftp