Click here to Skip to main content
15,915,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have program that need to move a file one directry to another directory on a FTP server Example (ftp://10.3.4.5.1/ftpfolder/folder\test.txt,ftp://10.3.4.5.1/ftpfolder/backup\test.txt)

C#
//This is the statement the OP really has:
//MoveFile("ftp://10.3.4.5.1", "anonymous", "", "/ftpfolder/", "/ftpfolder/backup\\", "test.txt");

//Editor changed to this because the code block formatting doesn't like \\"
MoveFile("ftp://10.3.4.5.1", "anonymous", "", "/ftpfolder/", < See Comment Above >, "test.txt");

public static string MoveFile(string ftpuri, string username, string password, string ftpfrompath, string ftptopath, string filename)
        {     
             string retval = string.Empty;      
            //FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpuri + ftpfrompath + filename);
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://10.14.21.80/ftpfolder/test.txt");
            ftp.Method = WebRequestMethods.Ftp.Rename;     
            ftp.Credentials = new NetworkCredential(username, password);     
            ftp.UsePassive = true;

//Statment OP really had was this:
//ftp.RenameTo = GetRelativePath("/ftpfolder/test.txt", "/Backup\\") + filename;

//Editor changed to this because the code block formatting doesn't like \\"
            ftp.RenameTo = GetRelativePath("/ftpfolder/test.txt", < See Comment Above >) + filename;


            Stream requestStream = ftp.GetRequestStream();      
            FtpWebResponse ftpresponse = (FtpWebResponse)ftp.GetResponse();      
            Stream responseStream = ftpresponse.GetResponseStream();      
            StreamReader reader = new StreamReader(responseStream);      
            return reader.ReadToEnd(); 
           
        }

 public static string GetRelativePath(string ftpBasePath, string ftpToPath) 
        {
          
            if (!ftpBasePath.StartsWith("/"))
            { 
                throw new Exception("Base path is not absolute");
            } 
            else 
             { 
                ftpBasePath = ftpBasePath.Substring(1); 
            } 
            if (ftpBasePath.EndsWith("/")) 
            { 
                ftpBasePath = ftpBasePath.Substring(0, ftpBasePath.Length - 1);
            } 
            if (!ftpToPath.StartsWith("/")) 
            { 
                throw new Exception("Base path is not absolute");
            } 
            else 
            { 
                ftpToPath = ftpToPath.Substring(1); 
            } 
            if (ftpToPath.EndsWith("/"))
            { 
                ftpToPath = ftpToPath.Substring(0, ftpToPath.Length - 1); 
            } 
            string[] arrBasePath = ftpBasePath.Split("/".ToCharArray()); 
            string[] arrToPath = ftpToPath.Split("/".ToCharArray()); 
            int basePathCount = arrBasePath.Count(); 
            int levelChanged = basePathCount; 
            for (int iIndex = 0; iIndex < basePathCount; iIndex++) 
            { 
                if (arrToPath.Count() > iIndex) 
            { 
                    if (arrBasePath[iIndex] != arrToPath[iIndex]) 
                    { levelChanged = iIndex; 
                      break; 
                    } 
                } 
            } 
            int HowManyBack = basePathCount - levelChanged; 
            StringBuilder sb = new StringBuilder(); 
            for (int i = 0; i < HowManyBack; i++) 
            {
                if (i == 0)
                {
                    sb.Append("ftp://10.3.4.5.1/ftpfolder/");
                }
            } for (int i = levelChanged;   i < arrToPath.Count();  i++) 
                { 
                    sb.Append(arrToPath[i]); //sb.Append("/"); 
                } 
            return sb.ToString(); 
         }


Errors : Cannot send a content-body with this verb-type.

Please any one help me on this issue. I am struggling to write code on this isssue. Thank you.

[Edit - Summarized subject and added code block]
[Second Edit - Commented out some code because code formatting on CP doesn't handle \\" quite right, and formatting was thrown off for the whole code block.]
Posted
Updated 25-Aug-20 9:16am
v3

I used below code which is working on my machine to move file on ftp directory
your request.RenameTo is the directory where you want to move file and Constants.FtpPathSrcFolder is your source directory.


C#
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + Constants.UserName + "@" + Constants.HostName + "/" + Constants.FtpPathSrcFolder + fileName);
             
request.Method = WebRequestMethods.Ftp.Rename;
request.Credentials = new NetworkCredential(Constants.UserName.Normalize(), Constants.Password.Normalize());
request.RenameTo = destFolder + fileName;   //Relative path 
FtpWebResponse response = (FtpWebResponse)request.GetResponse();


your paths should be "/Outgoing/Processing/" these slashes paths.

Thanks,
Ashwin
 
Share this answer
 
v2
 
Share this answer
 
Comments
skathirmca1 16-Nov-11 6:23am    
It's not working.. thanks you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900