简体   繁体   English

在 SFTP 服务器上使用 WinSCP 通过 C# 创建目录

[英]Create directory through C# using WinSCP on SFTP server

We are writing a console application in C# to upload files, through WinSCP .NET assembly using SFTP protocol, to the file server.我们正在用 C# 编写一个控制台应用程序,通过 WinSCP .NET 程序集使用 SFTP 协议将文件上传到文件服务器。 I am able to connect to the server and place files to the server but not at the exact place I want.我能够连接到服务器并将文件放置到服务器,但不能放在我想要的确切位置。 Please find the code as below:请找到以下代码:

where哪里

path = \Repository\Scan\Java\ant\UAT
zippath = C:\Temp\UAT_17-11-2016-19_40_05.zip
sftppath = \Repository\Scan\Java\ant\UAT\UAT_17-11-2016-19_40_05.zip

ZIP file is getting placed at Repository folder level with name as RepositoryScanJavaantUATUAT_17-11-2016-19_40_05.zip . ZIP 文件被放置在Repository文件夹级别,名称为RepositoryScanJavaantUATUAT_17-11-2016-19_40_05.zip If the directories don't exist on the server they are not getting created.如果服务器上不存在这些目录,则不会创建它们。

using (Session session = new Session())
{
    session.Open(sessionOptions);
    {
        if (System.IO.Directory.Exists(path))
        {
            Console.WriteLine("That path exists already.");
        }
        else
        {
            DirectoryInfo di = System.IO.Directory.CreateDirectory(path);
            Console.WriteLine(
                "The directory was created successfully at {0}.",
                System.IO.Directory.GetCreationTime(path));
        }

        try
        {
            Console.WriteLine("Put Files in the folder");
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Binary;
            TransferOperationResult transferResult;
            transferResult = session.PutFiles(zippath, sftppath, false, transferOptions);
            transferResult.Check();
        }                
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
  • You cannot use the System.IO.Directory.Exists to check an existence of SFTP directory, nor you can use the System.IO.Directory.CreateDirectory to create a directory on the SFTP server.您不能使用System.IO.Directory.Exists来检查 SFTP 目录是否存在,也不能使用System.IO.Directory.CreateDirectory在 SFTP 服务器上创建目录。 Use the WinSCP Session.FileExists and Session.CreateDirectory methods:使用 WinSCP Session.FileExistsSession.CreateDirectory方法:

     if (session.FileExists(path)) { Console.WriteLine("That path exists already."); } else { session.CreateDirectory(path); Console.WriteLine("The directory was created successfully"); }
  • SFTP paths use a slash, not a backslash: SFTP 路径使用斜杠,而不是反斜杠:

     path = /Repository/Scan/Java/ant/UAT sftppath = /Repository/Scan/Java/ant/UAT/UAT_17-11-2016-19_40_05.zip

暂无
暂无

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

相关问题 c# (WinSCP) 显示 sftp 目录结构与 c# 中的 treeview - c# (WinSCP) showing sftp directory structure with treeview in c# 使用 WinSCP C# 代码连接到 SFTP 服务器时出现异常 - PrivateKeyPassphrase 已设置,但未设置 SshPrivateKeyPath 和 TlsClientCertificatePath - Exception when connecting to SFTP server using WinSCP C# code - PrivateKeyPassphrase is set, but not SshPrivateKeyPath and TlsClientCertificatePath 如何使用C#中的WinSCP .NET程序集将内容从一个远程目录复制到同一服务器中的另一个目录 - How to copy things from one remote directory to another in the same server using WinSCP .NET assembly in C# 如何使用带有C#和WinSCP的模式获取远程目录的文件 - How to get the files of remote directory using a pattern with C# and WinSCP 使用C#中的WinSCP连接到SFTP时“没有可用的支持的身份验证方法” - “No supported authentication methods available” while connecting to SFTP using WinSCP in C# 如何使用密码或SSH指纹WinSCP C#.NET程序集进行SFTP身份验证 - How to SFTP authenticate using password or SSH fingerprint WinSCP C# .NET assembly 使用 WinSCP .NET 程序集更改根 SFTP 目录 - Change root SFTP directory using the WinSCP .NET assembly 使用 C# 以编程方式创建 SFTP 用户 - Create SFTP user programmatically using c# 无法在C#中使用SharpSSH更改sftp远程目录? - Cannot change sftp remote directory using SharpSSH in C#? 在 C# 中使用 SSH.NET SFTP 下载目录 - Downloading a directory using SSH.NET SFTP in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM