2013-08-07 2 views
3

FTP 사이트에서 .pgp 파일 만 다운로드해야하는 일정한 간격으로 콘솔 응용 프로그램을 실행해야합니다. FTP에있는 모든 pgp 파일을 다운로드해야합니다. 나는 FTP의 디렉토리 목록을 얻을 수있는 샘플 코드를 발견하고 여기에 쓴 : 나는 디렉토리 목록에서 유형 .pgp 디렉토리의 모든 파일을 다운로드하고에 저장하기 위해 무엇을해야C#을 사용하여 FTP에서 수많은 파일을 다운로드하는 방법?

FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://ourftpserver"); 
     req.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 

     req.Credentials = new NetworkCredential("user", "pass"); 

     FtpWebResponse response = (FtpWebResponse)req.GetResponse(); 

     Stream responseStream = response.GetResponseStream(); 
     StreamReader reader = new StreamReader(responseStream); 
     Console.WriteLine(reader.ReadToEnd()); 

     Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription); 

     reader.Close(); 
     response.Close(); 

우리 서버의 로컬 디렉토리?

+0

응답을 구문 분석하고 루프를 사용하십시오. – SLaks

+0

SLaks 코드 샘플을 좀 더 자세히 설명해 주시겠습니까? – SidC

답변

8

FtpWebRequestFtpWebResponse 개체가 정말 당신은 FTP 클라이언트를 찾고 (즉 등, 하나의 파일을 다운로드)

하나의 요청을해야합니까 설계되었습니다. .NET Framework에는 하나도 없지만 무료로 제공되는 것은 System.Net.FtpClient입니다. 이는 분명히 잘 작동합니다.

+0

대단히 감사합니다! 이 사이트, MSDN 및 다른 사이트에서 너무 많은 게시물을 읽었지 만 귀하의 정보는 직접적이고 중요한 것입니다. 매우 감사. 지금 다운로드 할 것입니다 :) – SidC

1

ftp에서 다운로드 파일을위한 코드.

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.0/my.txt"); 
     request.Method = WebRequestMethods.Ftp.DownloadFile; 
     request.Credentials = new NetworkCredential("userid", "pasword"); 
     FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
     Stream responseStream = response.GetResponseStream(); 
     FileStream file = File.Create(@c:\temp\my.txt); 
     byte[] buffer = new byte[32 * 1024]; 
     int read; 
     //reader.Read(

     while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0) 
     { 
      file.Write(buffer, 0, read); 
     } 

     file.Close(); 
     responseStream.Close(); 
     response.Close(); 
3

https://sshnet.codeplex.com/ 코드를 사용할 수있는 아주 좋은 도서관이있다 : 당신은 당신이 remoteFTPPath로 다운로드 할 위치에서 localFilesPath 및 FTP 폴더 경로로 파일을 다운로드 할 폴더 경로를 전달할 필요가.

public static void DownloadFilesFromFTP(string localFilesPath, string remoteFTPPath) 
     { 
      using (var sftp = new SftpClient(Settings.Default.FTPHost, Settings.Default.FTPUsername, Settings.Default.FTPPassword)) 
      { 
       sftp.Connect(); 
       sftp.ChangeDirectory(remoteFTPPath); 
       var ftpFiles = sftp.ListDirectory(remoteFTPPath, null); 
       StringBuilder filePath = new StringBuilder(); 
       foreach (var fileName in ftpFiles) 
       { 

        filePath.Append(localFilesPath).Append(fileName.Name); 
        string e = Path.GetExtension(filePath.ToString()); 
        if (e == ".csv") 
        { 
         using (var file = File.OpenWrite(filePath.ToString())) 
         { 
          sftp.DownloadFile(fileName.FullName, file, null); 
          sftp.Delete(fileName.FullName); 
         } 
        } 
        filePath.Clear(); 
       } 
       sftp.Disconnect(); 
      } 
     } 
+0

이 대답을 바탕으로 이것을 사용하기 시작했습니다. 매우 좋았습니다! – Codingo

1

Ultimate FTP 당신을 도울 수 있습니다.

using ComponentPro.IO; 
using ComponentPro.Net; 

... 

// Create a new instance. 
Ftp client = new Ftp(); 

// Connect to the FTP server. 
client.Connect("myserver"); 

// Authenticate. 
client.Authenticate("userName", "password"); 

// ... 

// Get all directories, subdirectories, and files from remote folder '/myfolder' to 'c:\myfolder'. 
client.DownloadFiles("/myfolder", "c:\\myfolder"); 

// Get all directories, subdirectories, and files that match the specified search pattern from remote folder '/myfolder2' to 'c:\myfolder2'. 
client.DownloadFiles("/myfolder2", "c:\\myfolder2", "*.pgp"); 

// or you can simply put wildcard masks in the source path, our component will automatically parse it. 
// download all *.pgp files from remote folder '/myfolder2' to local folder 'c:\myfolder2'. 
client.DownloadFiles("/myfolder2/*.pgp", "c:\\myfolder2"); 

// Download *.pgp files from remote folder '/myfolder2' to local folder 'c:\myfolder2'. 
client.DownloadFiles("/myfolder2/*.pgp", "c:\\myfolder2"); 

// Get files in the folder '/myfolder2' only. 
TransferOptions opt = new TransferOptions(true, RecursionMode.None, false, (SearchCondition)null, FileExistsResolveAction.Overwrite, SymlinksResolveAction.Skip); 
client.DownloadFiles("/myfolder2", "c:\\myfolder2", opt); 

// ... 

// Disconnect. 
client.Disconnect(); 

http://www.componentpro.com/doc/ftp이 더 많은 예제가 있습니다 다음 코드는 것을 보여줍니다.

+3

이것은 유료 라이브러리이며 위대한 라이브러리는 아닙니다. 피하십시오. 많은 좋은 무료 솔루션이 있습니다! – Codingo