C# 无法使用SFTP/SSH.NET从远程目录删除文件?
我正在创建一个带有多个文本框的C#windows窗体应用程序,并将用户输入保存为JSON。 我从JSON文件中检索用户输入,如下反序列化:C# 无法使用SFTP/SSH.NET从远程目录删除文件?,c#,json,winforms,C#,Json,Winforms,我正在创建一个带有多个文本框的C#windows窗体应用程序,并将用户输入保存为JSON。 我从JSON文件中检索用户输入,如下反序列化: string host2 = currentList[1].IPaddress; string username2 = currentList[1].username; string password2 = currentList[1].password; string remoteDirectory2 = currentList[1].sourcefold
string host2 = currentList[1].IPaddress;
string username2 = currentList[1].username;
string password2 = currentList[1].password;
string remoteDirectory2 = currentList[1].sourcefolder;
string localDirectory2 = currentList[1].destfolder;
string filextension2 = currentList[1].filextension;
string removedownloaded2 = currentList[1].removedownloaded.ToString();
这是我的json字符串结构。
{
"Record": 2,
"IPaddress": "192.168.6.247",
"Machinename": "taurus",
"username": "root",
"password": "root",
"sourcefolder": "/home/root/bin",
"destfolder": "D:/DataProfiler_Nautitech/Files",
"filextension": ".sh",
"removedownloaded": 1
}
目标如下:
if (removedownloaded2 == "1")
{
//First method
sftp2.Delete(path);
//Second method
sftp2.DeleteDirectory(path);
//Third method
sftp2.DeleteFile(path);
}
但这些文件都没有删除任何文件
这是完整代码:(失败!)
下载后如何删除文件,有什么想法吗?提前感谢您的帮助。您有什么例外吗?如果在foreach循环中取消对
catch block
中的代码的注释,是否会收到任何消息框?我建议你调试你的代码。你有没有找到解决方案?从那以后我就没做过,我没有答案。你有什么异常吗?如果在foreach循环中取消对catch block
中的代码的注释,是否会收到任何消息框?我建议调试你的代码。你有没有找到解决方案?从那以后我就没有做过,我还没有找到答案
using (SftpClient sftp2 = new SftpClient(host2, username2, password2))
{
try
{
sftp2.Connect();
Console.WriteLine("Machine 2 - Connected");
var files = sftp2.ListDirectory(remoteDirectory2);
foreach (var file in files)
{
try
{
string remoteFileName = file.Name;
if ((file.Name.EndsWith(filextension2)))
{
using (Stream file1 = File.OpenWrite(Path.Combine(localDirectory2, remoteFileName)))
{
string path = remoteDirectory2 + "/" + remoteFileName;
sftp2.DownloadFile(path, file1);
if (removedownloaded2 == "1")
{
//First method
sftp2.Delete(path);
//Second method
//sftp2.DeleteDirectory(path);
//Third method
//sftp2.DeleteFile(path);
}
}
}
}
catch (Exception er1)
{
//MessageBox.Show("An exception has been caught " + er1.ToString());
}
}
}
catch (Exception entry)
{
MessageBox.Show(entry.Message);
}
//finally
//{
// sftp2.Disconnect();
//}
}