2017-02-12 3 views
2

스택 오버플로에 대한 많은 게시물을 검토했지만 필요한 항목을 찾지 못했습니다. 나는 파일이확장명이 다른 SFTP 서버에 파일 업로드

temp.csv

내가 잘 작동하는 SFTP 서버에이 파일을 업로드한다고 가정합니다.

난 당신이 뭔가를 제안 해주십시오 수 temp.csv.ready

같은 다른 확장자로이 파일을 저장해야합니다.

다음은 코드로, 시도해 보았습니다. 하지만 파일 확장자를 변경할 수는 없습니다.

SessionOptions sessionOptions = new SessionOptions 
{ 
    Protocol = Protocol.Sftp, 
    HostName = System.Configuration.ConfigurationManager.AppSettings["puthost"].ToString(), 
    UserName = System.Configuration.ConfigurationManager.AppSettings["putusername"].ToString(), 
    Password = System.Configuration.ConfigurationManager.AppSettings["putpassword"].ToString(), 
    PortNumber = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["putport"].ToString()), 
    SshHostKeyFingerprint = ... //followed by your 16 bit key 
}; 
using (Session session = new Session()) 
{ 
    session.SessionLogPath = "log.txt"; 
    session.Open(sessionOptions); //Attempts to connect to your sFtp site 
    //Get Ftp File 
    TransferOptions transferOptions = new TransferOptions(); 
    transferOptions.TransferMode = TransferMode.Binary; //The Transfer Mode - 
    //<em style="font-size: 9pt;">Automatic, Binary, or Ascii 
    transferOptions.FilePermissions = null; //Permissions applied to remote files; 
    //null for default permissions. Can set user, 
    //Group, or other Read/Write/Execute permissions. 
    transferOptions.PreserveTimestamp = false; //Set last write time of 
    //destination file to that of source file - basically change the timestamp 
    //to match destination and source files. 
    transferOptions.ResumeSupport.State = TransferResumeSupportState.Off; 
    WinSCP.TransferOperationResult transferResult; 
    //the parameter list is: local Path, Remote Path, Delete source file?, transfer Options 
    transferResult = session.PutFiles(@System.Configuration.ConfigurationManager.AppSettings["sendfilesource"].ToString(), System.Configuration.ConfigurationManager.AppSettings["sendfiletarget"].ToString(), false, transferOptions); 
} 

답변

2

Session.PutFiles methodremotePath 인수는 다음과 같습니다

전체 경로에 파일을 업로드 할 수 있습니다.

그래서, 당신이 할 필요가 같이 전체 경로를 지정하는 것입니다 : 당신이 라이브러리를 사용하고자하는 경우

/remote/path/temp.csv.ready 
0

을, 당신은 3rd library like componenentpro sftp library 사용할 수 있습니다. client.UploadFile (@ "xxx \ temp.csv", "/temp.new.csv")을 사용하여 작업을 수행 할 수 있습니다. 이 코드 줄은 다음 코드 예제에서 발췌 한 것입니다.

// Create a new class instance. 
Sftp client = new Sftp(); 

// Connect to the SFTP server. 
client.Connect("localhost"); 

// Authenticate. 
client.Authenticate("test", "test"); 

// ... 

// Upload local file 'c:\test.dat' to '/test.dat'. 
client.UploadFile(@"xxx\temp.csv", "/temp.new.csv"); 

// ... 

// Disconnect. 
client.Disconnect();