dhamo
dhamo

Reputation: 181

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 (e.g., file not found, no access).

IF any other way move FTP files.

Please help me to resolve.

Upvotes: 3

Views: 6020

Answers (2)

K S Kiran
K S Kiran

Reputation: 31

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();

Upvotes: 3

Alastair Brown
Alastair Brown

Reputation: 1616

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();

Upvotes: 2

Related Questions