Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: SftpClient connect exception "No existing connection"

$
0
0
Hello

I did a small program, below, that does a SftpClient connect call
When I run the program on
a Windows 10 PC that I have administrator permissions, all works built with Visual Studio 2015

When I run the program on a Windows 7 PC (Visual Studio 2013) that I do not have administrator permissions,
I get an exception after the "connect" call

"no existing connection. Will make new connection to server 127.0.0.1
Exception has been thrown by the target of an invocation"

I am using
SftpClient sftp = new SftpClient("127.0.0.1", username, password);
sftp.Connect();
the differences between the 2 cases seems to be only
1) administrator permissions
2) Windows 10 versus 7
3) Visual Studio 2015 versus 2013


the complete program
namespace SftpServerConnection
{
  class SftpServerConnection
  {
    private string address;
    private string username;
    private string password;
    private string directory;

    ///////////////////////////////////////////////////////////////////////////////////////
    //SftpServerConnection
    ///////////////////////////////////////////////////////////////////////////////////////

    public SftpServerConnection(string address, string username, string password, string directory)
    {
      this.address = address;
      this.username = username;
      this.password = password;
      this.directory = directory;
    }

    ///////////////////////////////////////////////////////////////////////////////////////
    //SftpServerConnection::list
    ///////////////////////////////////////////////////////////////////////////////////////

    public bool list()
    {
      SftpClient sftp = new SftpClient(address, username, password);
      sftp.Connect();
      Console.WriteLine("connected to {0} ...", address);

      var files = sftp.ListDirectory(directory);
      Console.WriteLine("listing directory:");
      foreach (var file in files)
      {
        Console.WriteLine(file.FullName);
      }

      sftp.Disconnect();
      return true;
    }

    ///////////////////////////////////////////////////////////////////////////////////////
    //SftpServerConnection::send
    ///////////////////////////////////////////////////////////////////////////////////////

    public bool send(string fileName)
    {
      SftpClient sftp = new SftpClient(address, username, password);
      sftp.Connect();
      Console.WriteLine("connected to {0} ...", address);

      sftp.ChangeDirectory(directory);
      FileStream fileStream = new FileStream(fileName, FileMode.Open);
      Console.WriteLine("Uploading {0} ({1:N0} bytes)", fileName, fileStream.Length);
      sftp.UploadFile(fileStream, fileName);

      sftp.Disconnect();
      return true;
    }

    ///////////////////////////////////////////////////////////////////////////////////////
    //Main
    ///////////////////////////////////////////////////////////////////////////////////////

    static int Main(string[] args)
    {
      if (args.Length != 2)
      {
        System.Console.WriteLine("Please enter host and password separated by a space...");
        return 1;
      }

      string host = args[0];
      string password = args[1];
      string username = "pvn";
      string directory = ".";
      SftpServerConnection sftp_connection = new SftpServerConnection(
        host,
        username,
        password,
        directory);

      sftp_connection.send("test.txt");
      sftp_connection.list();
      return 0;
    }
  }
}

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>