简体   繁体   中英

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. 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.

My question is how can I do this using .NET and the respective winscpnet library?

The problem is the root directory of the session is different to remote path.

Example:

Session directory is /C/Document/ .

Remote path is /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/ .

Is there a way to set session root path in C#?

Solved: you are right it is a virtual path so /c/Inetpub instead of c/Inetpub

WinSCP .NET assembly does not use the concept of a working directory. 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.

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

the modern way of doing things would be, using Renci.SshNet. Dont forget to use regular slashes and to quit your session after things are done.

you can create your Session with:

    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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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