Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用SFTP/SSH.NET从远程目录删除文件?_C#_Json_Winforms - Fatal编程技术网

C# 无法使用SFTP/SSH.NET从远程目录删除文件?

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

我正在创建一个带有多个文本框的C#windows窗体应用程序,并将用户输入保存为JSON。 我从JSON文件中检索用户输入,如下反序列化:

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
  }
目标如下:

  • 连接到SFTP服务器

  • 将文件下载到本地服务器

  • 如果removedownloaded==1,则删除这些文件

  • 如果removedownload==0,则保留这些文件

  • 我尝试了以下方法

    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();
                    //}
                }