Realman's World

[C#] FTP 관련 클래스 본문

Language/C#

[C#] FTP 관련 클래스

리얼맨 2008. 9. 10. 12:35

C#에서 제공하는 FTP 관련 클래스는 여러가지가 있다.
그 중에서 가장 사용하기 쉬운 FtpWebRequest 클래스를 소개하려고 한다.


FtpWebRequest가 제공하는 기능은 WebRequestMethods 클래스의 멤버 변수를 살펴보면 된다.


  Name Description
AppendFile Represents the FTP APPE protocol method that is used to append a file to an existing file on an FTP server.
DeleteFile Represents the FTP DELE protocol method that is used to delete a file on an FTP server.
DownloadFile Represents the FTP RETR protocol method that is used to download a file from an FTP server.
GetDateTimestamp Represents the FTP MDTM protocol method that is used to retrieve the date-time stamp from a file on an FTP server.
GetFileSize Represents the FTP SIZE protocol method that is used to retrieve the size of a file on an FTP server.
ListDirectory Represents the FTP NLIST protocol method that gets a short listing of the files on an FTP server.
ListDirectoryDetails Represents the FTP LIST protocol method that gets a detailed listing of the files on an FTP server.
MakeDirectory Represents the FTP MKD protocol method creates a directory on an FTP server.
PrintWorkingDirectory Represents the FTP PWD protocol method that prints the name of the current working directory.
RemoveDirectory Represents the FTP RMD protocol method that removes a directory.
Rename Represents the FTP RENAME protocol method that renames a directory.
UploadFile Represents the FTP STOR protocol method that uploads a file to an FTP server.
UploadFileWithUniqueName Represents the FTP STOU protocol that uploads a file with a unique name to an FTP server.

* 참조 : http://msdn.microsoft.com/en-us/library/system.net.webrequestmethods.ftp_members.aspx 

위에 멤버에서 제공하는 기능을 선택하면된다.
예를 들어 특정 파일의 크기를 알고 싶다면 아래와 같이 사용하면 된다.

Uri ftpUri = new Uri(g_sServerFilePath);

FtpWebRequest reqFtp = (FtpWebRequest)WebRequest.Create(ftpUri);
reqFtp.Method = WebRequestMethods.Ftp.GetFileSize;
reqFtp.Credentials = new NetworkCredential(g_sFtpUserId, g_sFtpPwd);
reqFtp.Timeout = 1000;
FtpWebResponse resFtp = (FtpWebResponse)reqFtp.GetResponse();

String sFileSize = resFtp.ContentLength.ToString();

이때 g_sServerFilePath 은 Ftp에 있는 특정 파일의 Full Directory Path를 적어줘야 한다.

그런데 위의 클래스 중 파일의 upload와 download는 다른 클래스를 사용하는 것이 편리하다.
특히 다운로드시 진행 상태가 어떤지를 화면에 표시해 주고 싶을 경우 이벤트 핸들러를 제공해 준다.
WebClient라는 클래스가 바로 그것이다.
특정 파일을 다운로드 받을 경우 아래와 같이 사용하면 된다.


Uri ftpUri = new Uri(g_sServerFilePath);

WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(g_sFtpUserId, g_sFtpPwd);
wc.
DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.
DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadFileAsync(ftpUri, g_sTmpFilePath);

위의 소개한 바와 같이 다운로드의 진행 사항을 화면에 표시해 주고 싶을 경우 DownloadFileAsync 함수를 사용하고 DownloadProgressChanged 이벤트에 AsyncCompletedEventHandler 함수를 추가시킨 후 특정 작업을 수행하면 된다.
      
private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
       lblDownSize.Text = e.BytesReceived.ToString();
       pbDownload.Value = Convert.ToInt32(Convert.ToDouble(e.BytesReceived) / Convert.ToDouble(lblTotalSize.Text) * 100);
}

위와 같이 현재까지 받은 파일의 용량을 나타내 주는 기능을 추가할 있다.

또한 파일의 완료시 특정 기능을 수행되도록 하기 위해서는
DownloadFileCompleted 이벤트에 AsyncCompletedEventHandler 함수를 추가시키면 된다.
Comments