c# .net sftp 연동  C# / Language 

2015. 5. 12. 17:12

복사https://blog.naver.com/marnet/220357383255

번역하기 전용뷰어 보기

첨부파일 (3)

C#.NET FTP 파일 동기화 에이전트를 만드는 데 SFTP도 지원을 해주기로 함.


.NET에서는 기본적으로 SFTP는 지원을 하지 않는다고 한다.


그래서 서드파티 모듈을 써야 하는 데 구글링해보니 여러 가지가 있다. 


그 중 SharpSSH가 사장 사용하기 쉽고 접근성이 높은 거 같아 사용하기로 함.


SharpSSH 라이브러리는 아래의 주소에서 다운로드 받을 수 있다.


http://sourceforge.net/projects/sharpssh/


라이브러리는 다운 받고 진행 중인 프로젝트에 Tamir.SharpSSH.dll 파일을 참조하고 import 해준다.
 

다운로드, 업로드, 리스트 가져오기 소스 코드는 다음과 같다.


리스트 가져오기
1
2
3
4
5
6
7
8
9
string _ftpURL = "testsftp.com"//Host URL or address of the SFTP server
string _UserName = "admin";     //User Name of the SFTP server
string _Password = "admin123";  //Password of the SFTP server
int _Port = 22;                 //Port No of the SFTP server (if any)
string _ftpDirectory = "Receipts"//The directory in SFTP server where the files are present
Sftp oSftp = new Sftp(_ftpURL, _UserName, _Password);
oSftp.Connect(_Port);
ArrayList FileList = oSftp.GetFileList(_ftpDirectory);
oSftp.Close();

 


 

다운로드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string _ftpURL = "testsftp.com"//Host URL or address of the SFTP server
string _UserName = "admin";     //User Name of the SFTP server
string _Password = "admin123";  //Password of the SFTP server
int _Port = 22;                 //Port No of the SFTP server (if any)
string _ftpDirectory = "Receipts"//The directory in SFTP server where the files are present
string LocalDirectory = "D:\\FilePuller"//Local directory where the files will be downloaded
 
Sftp oSftp = new Sftp(_ftpURL, _UserName, _Password);
oSftp.Connect(_Port);
ArrayList FileList = oSftp.GetFileList(_ftpDirectory);
FileList.Remove(".");
FileList.Remove("..");        //Remove . from the file list
FileList.Remove("Processed"); //Remove folder name from the file list. If there is no folder name remove the code.
 
for (int i = 0; i < FileList.Count; i++)
{
    if (!File.Exists(LocalDirectory + "/" + FileList[i]))
    {
        oSftp.Get(_ftpDirectory + "/" + FileList[i], LocalDirectory + "/" + FileList[i]);
        Thread.Sleep(100);
    }
}
oSftp.Close();


 



업로드

1
2
3
4
5
6
7
8
9
10
11
12
string _ftpURL = "testsftp.com"//Host URL or address of the SFTP server
string _UserName = "admin";      //User Name of the SFTP server
string _Password = "admin123";   //Password of the SFTP server
int _Port = 22;                  //Port No of the SFTP server (if any)
string _ftpDirectory = "Receipts"//The directory in SFTP server where the files will be uploaded
string LocalDirectory = "D:\\FilePuller"//Local directory from where the files will be uploaded
string FileName = "test.txt";    //File name, which one will be uploaded
 
Sftp oSftp = new Sftp(_ftpURL, _UserName, _Password);
oSftp.Connect(_Port);
oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory + "/" + FileName);
oSftp.Close()



참고

http://www.codeproject.com/Questions/544479/C-plussftpplusdownloadplusandplusupload

http://cybarlab.com/download-upload-files-from-sftp-server-in-c-sharp

[출처] c# .net sftp 연동|작성자 주용준



+ Recent posts