我正在使用Visual Studio和C#Web应用程序。我正在尝试使用SSH.NET SshClient
将文件从Windows服务器移动到远程Linux服务器。我正在尝试下面的代码,但是文件没有被复制。
var filespath = FUupload.PostedFile.FileName.Replace("\\", "/");
SshClient sshclient = new SshClient("hostname", "username", "pwd");
sshclient.Connect();
ShellStream stream = sshclient.CreateShellStream("cmsd", 80, 24, 800, 600, 1024);
Label1.Text = sendCommand("sudo su - wwabc1", stream).ToString();
Label2.Text = sendCommand("whoami", stream).ToString();
Label3.Text = sendCommand("cp /" + filespath + " /wwabc1/test/folder_one/test/", stream).ToString();
public StringBuilder sendCommand(string customCMD)
{
StringBuilder answer;
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.AutoFlush = true;
WriteStream(customCMD, writer, stream);
answer = ReadStream(reader);
return answer;
}
private void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
{
writer.WriteLine(cmd);
while (stream.Length == 0)
{
Thread.Sleep(500);
}
}
private StringBuilder ReadStream(StreamReader reader)
{
StringBuilder result = new StringBuilder();
string line;
while ((line = reader.ReadLine()) != null)
{
result.AppendLine(line);
}
return result;
}
我没有收到任何错误,但是文件没有移动。
答案 0 :(得分:0)
您不能使用Shell命令在本地计算机和远程计算机之间传输文件。
想象一下,您正在使用SSH终端客户端(行PuTTY)。您可以在PuTTY中使用tsconfig.json
命令上传文件吗? -你不能。
您必须使用SFTP协议。
"files": [
"src/custom.d.ts"
]