简体   繁体   中英

How to move FTP files to another directory in C#?

I tried below code to move FTP files from one location to another location but I am facing issues.

Code:

Uri serverFile = new Uri("ftp://3.222.001.114/ftproot/Incomming/ProcessedFiles/Test.xml");
                FtpWebRequest reqFTP= (FtpWebRequest)FtpWebRequest.Create(serverFile);
                reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                reqFTP.Credentials = new NetworkCredential("ftpuser", "test123");
                reqFTP.RenameTo = "ftp://3.222.001.114/ftproot/Incomming/ProcessedFiles/Test/Test.xml";
                reqFTP.GetResponse().Close();

But I am getting below error:

Additional information: The remote server returned an error: (550) File unavailable (eg, file not found, no access).

IF any other way move FTP files.

Please help me to resolve.

Try this:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://3.222.001.114/ftproot/Incomming/ProcessedFiles/Test.xml");
request.Method = WebRequestMethods.Ftp.Rename;
request.Credentials = new NetworkCredential("ftpuser", "test123");
request.RenameTo = "../Test/Test.xml";   //Relative path 
FtpWebResponse response = (FtpWebResponse)request.GetResponse();

I think your problem is because FTP expects relative paths for RenameTo . Try this:

Uri serverFile = new Uri("ftp://3.222.001.114/ftproot/Incomming/ProcessedFiles/Test.xml");
FtpWebRequest reqFTP= (FtpWebRequest)FtpWebRequest.Create(serverFile);
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.Credentials = new NetworkCredential("ftpuser", "test123");
reqFTP.RenameTo = "Test/Test.xml";
reqFTP.GetResponse().Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM