使用临时文件名与WinSCP .NET / COM一起上传文件 [英] File upload with WinSCP .NET/COM with temporary filenames

查看:1224
本文介绍了使用临时文件名与WinSCP .NET / COM一起上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#中创建一个小型.NET应用程序,以将文件上传到FTP服务器。我在使用WinSCP的.NET DLL的同时这样做,我一直在试图找到一个很好的解决方案,我的问题。



FTP文件夹,我将把我所有的文件将被另一个应用程序监视。然后,这个应用程序会自动处理这些文件并自动处理。



所以我想避免的是在传输完成之前我的文件被应用程序占用。 p>

因此,我想使用临时文件名或临时文件夹,然后在上传完成后移动文件。



你认为最好的方法是什么?
第二个问题是,在WinSCP .NET中,应该有一个Transfer Resume选项,用于传输临时名称的文件并在完成时重命名。但我似乎无法得到这个工作,并寻找任何提示如何让这个工作?

正确的是,WinSCP的转移到临时文件名功能看起来像

它使得WinSCP上传文件的名称附加了 .filepart ,剥离了扩展名。

  TransferOptions transferOptions = new TransferOptions(); 
transferOptions.ResumeSupport.State = TransferResumeSupportState.On;
session.PutFiles(@d:\toupload\myfile.dat,/ home / user /,false,transferOptions).Check();

尽管它仅支持SFTP协议。






使用FTP协议,您必须手动执行此操作。

  session.PutFiles(@d:\toupload\myfile.dat,/home/user/myfile.dat.filepart)。 
session.MoveFile(/ home / user / myfile.dat.filepart,/home/user/myfile.dat);

如果您要上传多个文件,可以使用操作掩码,并迭代由 Session.PutFiles TransferOperationResult ,调用 Session.MoveFile

  TransferOperationResult transferResult; 
transferResult = session.PutFiles(@d:\toupload\ * .dat,/home/user/*.filepart)

//抛出任何错误
transferResult.Check();

//重命名上传的文件
foreach(TransferResult.Transfers中的TransferEventArgs传输)
{
string finalName = transfer.Destination.Replace(。filepart, .DAT);
session.MoveFile(transfer.Destination,finalName);



$ b

在文章上传/上传到临时文件名时锁定文件






另请参阅 SFTP文件锁定机制(适用于FTP)以适用于不同的方法隐藏正在上传的文件。

I am creating a small .NET application in C# to upload files to an FTP Server. I am using the .NET DLL for WinSCP while doing this and i have been trying to find a good solution to my problem.

The FTP folder where I will put all my files will be monitored by another application. This application will then take these files and process them automatically.

So what I want to avoid is that my files are grabbed by the application before the transfer is complete.

So I want to use either temporary filename usage or maybe a temporary folder and then move the files when upload is finished.

What do you suggest as the best approach? And second question is, in WinSCP .NET there should be an option for Transfer Resume that transfers the file with temporary name and renames when completed. But I cant seem to get this to work and are looking for any hints on how to get this to work?

解决方案

You are right that the "transfer to temporary file name" feature of WinSCP looks like the way to go.

It makes WinSCP upload file with the .filepart appended to its name, stripping the extension once done.

TransferOptions transferOptions = new TransferOptions();
transferOptions.ResumeSupport.State = TransferResumeSupportState.On;
session.PutFiles(@"d:\toupload\myfile.dat", "/home/user/", false, transferOptions).Check();

Though it is supported with an SFTP protocol only.


With an FTP protocol, you have to do this manually.

session.PutFiles(@"d:\toupload\myfile.dat", "/home/user/myfile.dat.filepart").Check();
session.MoveFile("/home/user/myfile.dat.filepart", "/home/user/myfile.dat");

If you are uploading multiple files, you can use an operation mask, and iterate list of successfully uploaded files returned by the Session.PutFiles in TransferOperationResult, calling the Session.MoveFile for each.

TransferOperationResult transferResult;
transferResult = session.PutFiles(@"d:\toupload\*.dat", "/home/user/*.filepart")

// Throw on any error
transferResult.Check();

// Rename uploaded files
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
    string finalName = transfer.Destination.Replace(".filepart", ".dat");
    session.MoveFile(transfer.Destination, finalName);
}

There's also a complete PowerShell example in the article Locking files while uploading / Upload to temporary file name.


See also SFTP file lock mechanism (applies to FTP as well) for different approaches to hiding files being uploaded.

这篇关于使用临时文件名与WinSCP .NET / COM一起上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆