将文件上传到具有不同扩展名

时间:2017-02-12 10:38:24

标签: c# sftp winscp winscp-net

我在Stack Overflow上发了很多帖子,但是我还没找到我需要的东西。

  

我有一个假设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 个答案:

答案 0 :(得分:2)

Session.PutFiles methodremotePath参数是:

  

将文件上传到的完整路径。

因此,您需要做的就是指定完整路径,如:

/remote/path/temp.csv.ready

答案 1 :(得分: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();