所以我有这样的代码:
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'
我做错了什么?这是文件所在的位置,我是管理员。
发布于 2019-02-06 21:37:51
在Windows中,文件夹分隔符是反斜杠"\"
而不是斜杠"/"
而是使用
client.WriteAllText(@"C:\Users\Public\Documents\Test.json", "jeff");
// ^---------------------- Notice the @ that will automatically escape the \
https://stackoverflow.com/questions/54554857
复制相似问题