0

SSH コマンドの実行とファイルのアップロードを既存のアプリケーションに追加しています。さまざまな無料およびライセンス付きのライブラリを比較した後、http://code.google.com/p/ssh2-net/を使用することにしました。これは、JAVA ライブラリの C# への移植と思われるためです。私たちはすでにJAVAバージョンで良い経験をしてきました...

ファイルのアップロードがうまくいきません...誰か助けてくれますか? これは、私が見つけたいくつかのサンプルの組み合わせである私のコードです

Connection conn = StartConnection();    
// --> conn checked and is open!

Session session = conn.openSession();
session.execCommand("sudo mount -o remount,rw /");
// --> no error

SFTPv3Client client = new SFTPv3Client(conn);
// --> this throws error "Cannot access a closed Stream."

SFTPv3FileHandle handle = client.createFile("/tmp/" + sshSource.Name);

using (StreamReader sr = new StreamReader(sshSource.FullName)) 
{
    char[] buffer = new char[1024];
    int offset = 0;

    while (sr.Peek() >= 0)
    {
        int i = sr.Read(buffer, offset, buffer.Length);

        byte[] bytes = new byte[1024];
        for (int j = 0; j < 1024; j++)
            bytes[j] = (byte) buffer[j];

        client.write(handle, offset, bytes, 0, i);
        offset += i;
    }
}

コメントで述べたように、クライアントが定義されている行に既にエラーがあります SFTPv3Client client = new SFTPv3Client(conn);

助けてくれてありがとう、フランク

4

1 に答える 1

0

SharpSshに切り替え、C#のより良い(より簡単な)ソリューションのようです

http://www.tamirgal.com/blog/page/SharpSSH.aspx

SshTransferProtocolBase tx = new Scp(_hostname, _username, _password);
SshExec exec = new SshExec(_hostname, _username, _password);

tx.Connect();
exec.Connect();

exec.RunCommand("sudo mount -o remount,rw /");
exec.RunCommand("sudo rm /tmp/" + Path.GetFileName(sshTarget));
tx.Put(sshSource.FullName, "/tmp/" + Path.GetFileName(sshTarget));
于 2012-11-21T14:30:16.327 に答える