简体   繁体   English

使用 WinSCP .NET 程序集上传后删除文件

[英]Delete files after upload with WinSCP .NET assembly

My script is very simple我的脚本很简单

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = conHostName,
    UserName = conUserName,
    Password = conPasswort,
    SshHostKeyFingerprint = conSshHostKeyFingerprint
};

using (Session session = new Session())
{
    session.Open(sessionOptions);

    TransferOptions transferOptions = new TransferOptions();
    transferOptions.TransferMode = TransferMode.Binary;

    TransferOperationResult transferResult;

    if(modus == "download")
    {
        transferResult = session.GetFiles(
            remoteDirectoryDownload, localDirectoryDownload, false, transferOptions);
    } 
    else
    {
        transferResult = session.PutFiles(
            localDirectoryUpload, remoteDirectoryUpload, false, transferOptions);
    }

    transferResult.Check();

    foreach (TransferEventArgs transfer in transferResult.Transfers)
    {
        try
        {
            Console.WriteLine("Delete File: {0}", transfer.FileName);
            session.RemoveFiles(transfer.FileName);
        }
        catch (Exception e)
        {

            Console.WriteLine("Error: {0}", e);
        }
    }
}

When I do the download, then the files from the remote host would be transferred and deleted on the remote host.当我进行下载时,来自远程主机的文件将在远程主机上传输和删除。 But when I make the upload from the local folder, then the uploads would work but the file on the local folder would not be deleted.但是当我从本地文件夹进行上传时,上传会起作用,但不会删除本地文件夹中的文件。 I get no error.我没有错误。

I start this with my user.我从我的用户开始。 I am the owner of this files and I can delete them self manually.我是这些文件的所有者,我可以手动删除它们。

Also when I set a breakpoint on session.RemoveFiles , then the correct file with the correct local path would be shown.此外,当我在session.RemoveFiles上设置断点时,将显示具有正确本地路径的正确文件。

Maybe you have a idea for me where my fail is.也许你对我的失败有什么想法。

Session.RemoveFiles deletes remote files only. Session.RemoveFiles仅删除远程文件。 It cannot delete local files.它不能删除本地文件。 For deleting local files, use the standard .NET File.Delete .要删除本地文件,请使用标准 .NET File.Delete

Though easier (and better) is to pass true to the remove (3rd) argument of Session.GetFiles / Session.PutFiles .虽然更容易(更好)是将true传递给Session.GetFiles / Session.PutFilesremove (第三个)参数。

It's better than your current code even for downloads, as it deletes all successfully downloaded files.即使对于下载,它也比您当前的代码更好,因为它会删除所有成功下载的文件。 While your current code won't delete any files, if only part of the files are downloaded successfully.虽然您当前的代码不会删除任何文件,但如果只有部分文件成功下载。

Also, you are using the default TransferOptions , so that code is redundant.此外,您使用的是默认TransferOptions ,因此该代码是多余的。

In the end, you can simplify your code to:最后,您可以将代码简化为:

using (Session session = new Session())
{
    session.Open(sessionOptions);

    if (modus == "download")
    {
        session.GetFiles(remoteDirectoryDownload, localDirectoryDownload, true).Check();
    } 
    else
    {
        session.PutFiles(localDirectoryUpload, remoteDirectoryUpload, true).Check();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM