Click here to Skip to main content
15,885,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi There,

I have created a windows .net service. which is used to move the files from (uri/fiolder1/filename) to (uri/fiolder1/Archive/filename) of ftp. My code is as follows:
C#
FtpWebRequest Wr = null;
NetworkCredential User = null;
try
{
  WriteLog("Processing Archiving on FTP: File Name: " + File);
  User = new NetworkCredential(FTPUser, FTPPass);
  Wr = (FtpWebRequest)FtpWebRequest.Create(FTPUri + File);
  Wr.UseBinary = true;
  Wr.KeepAlive = false;
  Wr.Method = WebRequestMethods.Ftp.Rename;
  Wr.Credentials = User;
  //Wr.RenameTo = "Archive/"  + DateTime.Now.AddHours(-11).ToString("dd-MM-yyyy_HH_mm_ss") +"_" +File;
  Wr.RenameTo = "Archive/" + DateTime.Now.ToString("dd-MM-yyyy_HH_mm_ss") + "_" + File;
  FtpWebResponse back = (FtpWebResponse)Wr.GetResponse();
  //bool Success = back.StatusCode == FtpStatusCode.CommandOK || back.StatusCode == FtpStatusCode.FileActionOK;
  back.Close();
  Wr = null;
  User = null;
  
  return true;
}
catch (Exception ex)
{
  WriteLog("Err at MoveFileToArchiveOnFTP :>" + ex.Message);
  return false;
}

It is working fine but some time it throws an error:The remote server returned an error: (450) File unavailable (e.g., file busy).

So please help me to resolve this error ASAP.
Posted
Updated 10-Sep-14 6:16am
v2

1 solution

C#
//strFTPIP is address of FTP Server and strFile is Filename
var ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(strFTPIP + strFile);

ftpWebRequest.KeepAlive = false;
ftpWebRequest.UseBinary = true;

//strFTPuserName is username of ftp server and strFTPPassword is password of ftp server
ftpWebRequest.Credentials = new NetworkCredential(strFTPuserName, strFTPPassword);
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;

//filepath is path of the file in local server
using (var inputStream = File.OpenRead(filepath))
using (var outputStream = ftpWebRequest.GetRequestStream())
{
        var buffer = new byte[4096];
        int totalReadBytesCount = 0;
        int readBytesCount;
        while ((readBytesCount = inputStream.Read(buffer, 0, buffer.Length)) > 0)
        {
                outputStream.Write(buffer, 0, readBytesCount);
                totalReadBytesCount += readBytesCount;
                var progress = totalReadBytesCount * 100.0 / inputStream.Length;
        }
}

ftpWebRequest.Abort();
 
Share this answer
 
v2

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