Brave Soul
Brave Soul

Reputation: 3620

Upload file to SFTP server with different extension

I went through many posts on Stack Overflow, but I have not found what I needed.

I have a file suppose temp.csv

I want to upload this file to an SFTP server which is working fine.

I need to save this file with different extension like temp.csv.ready

Can you please suggest something on this.

Here is the code, which I tried and working fine. But I'm not not able to change file extension.

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);
}

Upvotes: 2

Views: 1853

Answers (2)

Roberto12cr
Roberto12cr

Reputation: 15

If you are willing to use a library, you may want to use a 3rd library like componenentpro sftp library. You can accomplish the job with client.UploadFile(@"xxx\temp.csv", "/temp.new.csv"). That line of code is excerpt from the following code example:

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

Upvotes: 0

Martin Prikryl
Martin Prikryl

Reputation: 202652

The remotePath argument of the Session.PutFiles method is:

Full path to upload the file to.

So, all you need to do, is to specify the full path like:

/remote/path/temp.csv.ready

Upvotes: 2

Related Questions