所以我有这个代码:
public static void UploadSFTPFile(string host, string username,
string password, string sourcefile, string destinationpath, int port)
{
string Ip = "";
String strHostName = string.Empty;
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Ip = (addr[i].ToString());
}
using (SftpClient client = new SftpClient(host, port, username, password))
{
client.Connect();
client.ChangeDirectory(destinationpath);
using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
{
client.BufferSize = 4 * 1024;
client.UploadFile(fs, Path.GetFileName(sourcefile));
}
client.WriteAllText("C:/Users/Public/Documents/192.168.0.112.json", "jeff");
}
}
我使用这种方法在sftp服务器中上传文件。 这很完美,但是我也尝试在文件中写一些东西:
client.WriteAllText("C:/Users/Public/Documents/Test.json", "jeff");
这是我提示错误消息的地方:
Renci.SshNet.Common.SftpPathNotFoundException: 'No such file'
我做错了什么,这是文件所在的位置,我是管理员。
答案 0 :(得分:0)
在Windows中,文件夹定界符是反斜杠"\"
而不是斜杠"/"
宁可使用
client.WriteAllText(@"C:\Users\Public\Documents\Test.json", "jeff");
// ^---------------------- Notice the @ that will automatically escape the \