.net winscp文件上传重复文件,文件名加上DD_MM_YY_HH_mm_SS

ui7jx7zq  于 2023-08-08  发布在  .NET
关注(0)|答案(2)|浏览(121)

我用下面的代码上传文件到sftp

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

                    using var s = new MemoryStream(Encoding.UTF8.GetBytes(content ?? ""));
                    TransferOptions o = new TransferOptions { OverwriteMode= OverwriteMode.Overwrite };
                    session.PutFile(s, remoteFilePath, o);

                    session.Close();
                    result = true;
                }

字符串
有时候我会无缘无故地看到重复文件,其后缀与上传时间(DD_MM_YY_HH_mm_ss)一致
知道是什么问题吗

aelbi1ox

aelbi1ox1#

我会再次尝试上传过程,使用官方的例子

using System;
using WinSCP;
 
class Example
{
    public static int Main()
    {
        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "example.com",
                UserName = "user",
                Password = "mypassword",
                SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
            };
 
            using (Session session = new Session())
            {
                // Connect
                session.Open(sessionOptions);
 
                // Upload files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
 
                TransferOperationResult transferResult;
                transferResult =
                    session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);
 
                // Throw on any error
                transferResult.Check();
 
                // Print results
                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
                }
            }
 
            return 0;
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e);
            return 1;
        }
    }
}

字符串
这样,您可以:

仔细检查是否存在任何formatting timestamp option in Batch File,即设置任何%TIMESTAMP%环境变量。

hs1ihplo

hs1ihplo2#

我所做的是,而不是单独的一个接一个的调用,我做的事情在一个循环,而不是打开会话,每次我打开它时,它没有打开

using (Session session = new Session())
                {
                    if (!session.Opened)
                    {
                        session.Open(sessionOptions);
                    }

                    foreach (var item in newContents)
                    {

                        try
                        {
                            if (!string.IsNullOrWhiteSpace(_config.DefaultPath))
                            {
                                item.SftpFileName = _config.DefaultPath + item.SftpFileName;
                            }

                            using var s = new MemoryStream(Encoding.UTF8.GetBytes(item.Content ?? ""));

                            session.PutFile(s, item.SftpFileName, o);

                            item.IsSuccess= true;
                        }
                        catch (Exception exception)
                        {
                            _logger.LogError(exception, $"Failed in uploading file to using winscp [{item.SftpFileName}]");
                        }
                    }
                    session.Close();
                }

字符串

相关问题