Wednesday, July 28, 2010

C#.NET FTP File Upload

C#.NET gives the namespace to upload Files to FTP System.NET following custom method can be use to upload a file in to a FTP Server
public static void UploadToFTP(string FTPAddress, string FileName, string FullPath, string UserName, string Password)
{
       /*
        * FTPAddress - URL or IP address of the FTP server
        * FileName - Name of the File which is going to upload
        * FullPath - You have to give the Absolute path to the File you are going to upload
        * UserName - User name for the FTP account
        * Password - FTP Password
        */
System.Net.FtpWebRequest objWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(string.Format("{0}/{1}", FTPAddress, FileName));

if (objWebRequest != null)
{
objWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
objWebRequest.Credentials = new System.Net.NetworkCredential(UserName, Password);
objWebRequest.UsePassive = true;
objWebRequest.UseBinary = true;
objWebRequest.KeepAlive = false;

byte[] FileBuffer = null;
//Load The File To Mememory

try
{
   //open and Read the File in To the Memory
   using (System.IO.FileStream objFStream = System.IO.File.OpenRead(FullPath))
  {
     FileBuffer = new byte[objFStream.Length];

    objFStream.Read(FileBuffer, 0, FileBuffer.Length);
    objFStream.Close();
  }

    //Upload File To the FTP
    using (System.IO.Stream objStream = objWebRequest.GetRequestStream())
   {
       if (FileBuffer != null)
       {
           objStream.Write(FileBuffer, 0, FileBuffer.Length);
           objStream.Close();
       }
    }
}
 catch (Exception Ex)
 {
    throw Ex;
 }
}


}

Did you get this Error Exception? “System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, …..Then your have to read this Post too

You will be able to download a good windows application to the this purpose from http://www.vcskicks.com/download-file-ftp.php that was really help full to me thanks should goes to the people who ever wrote that article






1 comment:

  1. This is a great FTP Library for C#, it is reasonably priced too:
    https://www.kellermansoftware.com/p-39-net-ftp-library.aspx

    ReplyDelete