Thứ Tư, 4 tháng 6, 2014

File Upload, Download, Delete from FTP Server Using C#

File Upload, Download, Delete from FTP Server Using C#

Some time we need to download, upload file from FTP server. Here is some example to FTP operation.
For this we need to include one namespace and it is. using System.Net


File Download from FTP Server

public void DownloadFile(string HostURL, string UserName, string Password, string SourceDirectory, string FileName, string LocalDirectory)
        {
            if (!File.Exists(LocalDirectory + FileName))
            {
                try
                {
                    FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create(HostURL + "/" + SourceDirectory + "/" + FileName);
                    requestFileDownload.Credentials = new NetworkCredential(UserName, Password);
                    requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
                    FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
                    Stream responseStream = responseFileDownload.GetResponseStream();
                    FileStream writeStream = new FileStream(LocalDirectory + FileName, FileMode.Create);
                    int Length = 2048;
                    Byte[] buffer = new Byte[Length];
                    int bytesRead = responseStream.Read(buffer, 0, Length);
                    while (bytesRead > 0)
                    {
                        writeStream.Write(buffer, 0, bytesRead);
                        bytesRead = responseStream.Read(buffer, 0, Length);
                    }
                    responseStream.Close();
                    writeStream.Close();
                    requestFileDownload = null;
                    responseFileDownload = null;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }




Show File List in FTP Server
public List<string> ShowFileList(string HostURL, string UserName, string Password, string SourceDirectory)

        {

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(HostURL + "/" + SourceDirectory);

            request.Credentials = new NetworkCredential(UserName, Password);

            request.Method = WebRequestMethods.Ftp.ListDirectory;
            StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
            List<string> lines = new List<string>();
            string line;
            while ((line = streamReader.ReadLine()) != null)
            {
                lines.Add(line);
            }
            return lines;
        }




Delete form FTP Server
public void DeleteFile(string HostURL, string UserName, string Password, string SourceDirectory, string FileName)
        {

            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(HostURL + "/" + SourceDirectory + "/" + FileName);
            ftpRequest.Credentials = new NetworkCredential(UserName, Password);
            ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
            FtpWebResponse responseFileDelete = (FtpWebResponse)ftpRequest.GetResponse();

        }



Move File from One Directory to Another in FTP Server

public void MoveFile(string HostURL, string UserName, string Password, string SourceDirectory, string DestinationDirectory, string FileName)
        {
            FtpWebRequest ftpRequest = null;
            FtpWebResponse ftpResponse = null;
            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create(HostURL + "/" + SourceDirectory + "/" + FileName);
                ftpRequest.Credentials = new NetworkCredential(UserName, Password);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.Rename;
                ftpRequest.RenameTo = DestinationDirectory + "/" + FileName;

                if (CheckFileExistOrNot(HostURL, UserName, Password, SourceDirectory + "/" + DestinationDirectory, FileName) == false)
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();            
              

                ftpResponse.Close();
                ftpRequest = null;
            }
            catch (Exception ex)
            {
                throw ex;             

          
            }
        }


Check File Existence in FTP Server

public bool CheckFileExistOrNot(string HostURL, string UserName, string Password, string SourceDirectory, string FileName)
        {
            FtpWebRequest ftpRequest = null;
            FtpWebResponse ftpResponse = null;
            bool IsExists = true;

            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create(HostURL + "/" + SourceDirectory + "/" + FileName);
                ftpRequest.Credentials = new NetworkCredential(UserName, Password);              
                ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;              
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpResponse.Close();
                ftpRequest = null;
            }
            catch (Exception ex)
            {
                IsExists = false;
            }
            return IsExists;
        }

Không có nhận xét nào:

Đăng nhận xét