简体   繁体   English

使用 WinSCP .NET 程序集更改根 SFTP 目录

[英]Change root SFTP directory using the WinSCP .NET assembly

I'm working for a client that has given me access to two specific folders and their subfolders only.我正在为一个客户工作,该客户只允许我访问两个特定文件夹及其子文件夹。 The first one used to be our previous working space and now we will switch to the second one.第一个曾经是我们以前的工作空间,现在我们将切换到第二个。

When I connect to the SFTP using the WinSCP GUI it connects me to the old folder.当我使用 WinSCP GUI 连接到 SFTP 时,它会将我连接到旧文件夹。 However, I can change that by clicking on settings and adding the “new” path in the remote path field.但是,我可以通过单击设置并在远程路径字段中添加“新”路径来更改它。 The session will then take me to the new default folder/workspace automatically when I connect.然后,当我连接时,session 将自动将我带到新的默认文件夹/工作区。

My question is how can I do this using .NET and the respective winscpnet library?我的问题是如何使用 .NET 和相应的 winscpnet 库来做到这一点?

The problem is the root directory of the session is different to remote path.问题是 session 的根目录与远程路径不同。

Example:例子:

Session directory is /C/Document/ . Session 目录为/C/Document/

Remote path is /C/Inetpub/ftproot/username/远程路径是/C/Inetpub/ftproot/username/

When I used the following command on terminal:当我在终端上使用以下命令时:

winscp.com> open sftp://someone:password;fingerprint=something@ipaddress/C/Inetpub/ftproot/username

winscp.com> put some.txt /in

winscp.com> exit

it works fine, Because as we can see, my session directory is /C/Inetpub/ftproot/username/ .它工作正常,因为我们可以看到,我的 session 目录是/C/Inetpub/ftproot/username/

Is there a way to set session root path in C#?有没有办法在 C# 中设置 session 根路径?

Solved: you are right it is a virtual path so /c/Inetpub instead of c/Inetpub已解决:你是对的,它是一个虚拟路径,所以 /c/Inetpub 而不是 c/Inetpub

WinSCP .NET assembly does not use the concept of a working directory. WinSCP .NET 程序集不使用工作目录的概念。 It always uses absolute paths.它总是使用绝对路径。

So if you have your GUI session configured to start in /new/path , use that as an absolute path in WinSCP .NET assembly.因此,如果您将 GUI 会话配置为从/new/path开始,请将其用作 WinSCP .NET 程序集中的绝对路径。

session.PutFiles(@"c:\local\path\*", "/new/path/", false, transferOptions);

the modern way of doing things would be, using Renci.SshNet.现代的做事方式是使用 Renci.SshNet。 Dont forget to use regular slashes and to quit your session after things are done.完成后不要忘记使用常规斜杠并退出 session。

you can create your Session with:您可以使用以下命令创建 Session:

    var connectionInfo = new Renci.SshNet.ConnectionInfo(host, username, new PasswordAuthenticationMethod(username, password));

    using (var sftp = new SftpClient(connectionInfo))
    {
        sftp.Connect();
        sftp.ChangeDirectory("..");
        sftp.ChangeDirectory("C:\Inetpub\ftproot\username\");
     }
     sftp.Disconnect();

暂无
暂无

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

相关问题 无法使用WinSCP .NET程序集将文件下载到目录中 - Unable to download file into directory using WinSCP .NET assembly 在 Linux 上使用 WinSCP .NET 组件 - Using WinSCP .NET assembly on Linux 使用WinSCP .NET程序集仅使用SFTP协议删除或移动所选文件 - Remove or move only selected files with SFTP protocol using WinSCP .NET assembly 使用 WinSCP .NET 程序集检查 SFTP 服务器上是否存在任何具有扩展名的文件 - Check if any file with an extension exists on SFTP server using WinSCP .NET assembly 使用WinSCP .NET程序集从SFTP服务器下载带有偏移量的文件块 - Download file chunks with offset from SFTP server using WinSCP .NET assembly 如何使用密码或SSH指纹WinSCP C#.NET程序集进行SFTP身份验证 - How to SFTP authenticate using password or SSH fingerprint WinSCP C# .NET assembly WinSCP .NET程序集-非递归GetFiles根目录(无子目录) - WinSCP .NET assembly - GetFiles root directory non-recursive (without subdirs) WinSCP .NET程序集:目录同步中的FileMask不起作用 - WinSCP .NET assembly: FileMask in Directory Synchronization not working 在 SFTP 服务器上使用 WinSCP 通过 C# 创建目录 - Create directory through C# using WinSCP on SFTP server 如何使用C#中的WinSCP .NET程序集将内容从一个远程目录复制到同一服务器中的另一个目录 - How to copy things from one remote directory to another in the same server using WinSCP .NET assembly in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM