2017-02-12 11 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

remotePath аргумент Session.PutFiles method является:

Полный путь, чтобы загрузить файл.

Таким образом, все, что вам нужно сделать, это указать полный путь:

/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();