Steve Gaines
Steve Gaines

Reputation: 601

FTP Copy And Delete Not Working

I have created a process in C# that is intended to move files around by FTP. One part is working - my GetFileFTP. I can retrieve a file from the FTP server and place a copy on my local machine. I also want to copy a file to an archive folder on the FTP server and then delete the original file on the FTP server. I created the functions CopyFileFTP and DeleteFileFTP shown below. They run without error, but the file does not get copied or deleted. I also check the StatusCode in the delete function and everything looks happy. Do you know why this isn't working? Could it be that I only have read-only permission on the server? Would it throw an error if I try to delete and don't have permission or would it just process the command with no action? My supervisor doesn't seem to want to ask the supplier who owns the FTP server if we have the necessary permissions, so I will try posting this question.

        public static bool CopyFileFTP(string sFileURI1, string sFileURI2, string sUserName, string sPassword)
        {
            bool bSuccess = false;
            try
            {
                FtpWebRequest oRequest1 = (FtpWebRequest)WebRequest.Create(sFileURI1);
                oRequest1.Credentials = new NetworkCredential(sUserName, sPassword);
                oRequest1.Method = WebRequestMethods.Ftp.DownloadFile;
                FtpWebResponse oResponse = (FtpWebResponse)oRequest1.GetResponse();
                Stream oStream1 = oResponse.GetResponseStream();

                MemoryStream oMemoryStream = new MemoryStream();
                byte[] aChunk = new byte[4096];
                int iBytesRead = 0;
                do
                {
                    iBytesRead = oStream1.Read(aChunk, 0, aChunk.Length);
                    if (iBytesRead > 0)
                    {
                        oMemoryStream.Write(aChunk, 0, iBytesRead);
                    }
                    else
                    {
                        break;
                    }
                } while (iBytesRead > 0);
                byte[] aBuffer = oMemoryStream.ToArray();

                FtpWebRequest oRequest2 = (FtpWebRequest)WebRequest.Create(sFileURI2);
                oRequest2.Credentials = new NetworkCredential(sUserName, sPassword);
                oRequest2.Method = WebRequestMethods.Ftp.UploadFile;
                Stream oStream2 = oRequest2.GetRequestStream();
                oStream2.Write(aBuffer, 0, aBuffer.Length);
                oStream2.Close();
                oStream2.Dispose();
                oStream1.Close();
                oStream1.Dispose();
                bSuccess = true;
            }
            catch (Exception oException)
            {
                LogEvent(oException.Message);
            }
            return bSuccess;
        }
        public static bool DeleteFileFTP(string sFileURI, string sUserName, string sPassword)
        {
            bool bSuccess = false;
            try
            {
                FtpWebRequest oRequest = (FtpWebRequest)WebRequest.Create(sFileURI);
                oRequest.Credentials = new NetworkCredential(sUserName, sPassword);
                oRequest.Method = WebRequestMethods.Ftp.DeleteFile;
                FtpWebResponse oResponse = (FtpWebResponse)oRequest.GetResponse();
                string sWelcomeMessage = oResponse.WelcomeMessage;
                string sStatusDescription = oResponse.StatusDescription;
                bool bIsMutuallyAuthenticated = oResponse.IsMutuallyAuthenticated;
                string sBannerMessage = oResponse.BannerMessage;
                FtpStatusCode eStatusCode = oResponse.StatusCode;
                string sExitMessage = oResponse.ExitMessage;
                oResponse.Close();
                bSuccess = true;
            }
            catch (Exception oException)
            {
                LogEvent(oException.Message);
            }
            return bSuccess;
        }

Upvotes: 0

Views: 692

Answers (1)

Russ Clarke
Russ Clarke

Reputation: 17909

It looks fine to me; the code is almost exactly as MSDN documents FtpWebRequest.

I would agree that it is credentials most likely.

Incidentally, have you tried using a traditional FTP client with the same credentials and see if you can perform these actions manually?

Additionally, it appears as you suspected that there is no exception thrown for not having permissions; you can catch an exception but that will mostly be from actual connection errors. You need to process the response's status to find out what the FTP server is telling you.

Upvotes: 1

Related Questions