[英]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.