Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: ScpClient missing methods

$
0
0

Hi, im sorry i dont know much about the SCP, the workaround i find is to create a SshShell and send commands, here are how i implement it:

 

///<summary>/// Runs the command.///</summary>///<param name="commandText">The command text.</param>///<exception cref="IT.FileTransfer.FileTransferException"></exception>void RunCommand(string commandText)
        {using (SshClient client = new SshClient(Host, User, Password))
            {
                client.Connect();using (SshCommand cmd = client.RunCommand(commandText))
                {if (cmd.ExitStatus != 0)thrownew FileTransferException(cmd.Error)
                        {
                            RemotePath = commandText
                        };
                }
            }
        }///<summary>/// Crea un directorio en el host remoto.///</summary>///<param name="remoteDirectory">El path del directorio remoto.</param>publicoverridevoid CreateDirectory(string remoteDirectory)
        {
            RunCommand(@"mkdir " + remoteDirectory);
        }///<summary>/// Elimina un directorio en el host remoto.///</summary>///<param name="remoteDirectory">El path del directorio remoto.</param>publicoverridevoid DeleteDirectory(string remoteDirectory)
        {if (DeleteRecursive)
                RunCommand(@"rm -r " + remoteDirectory);else
                RunCommand(@"rmdir " + remoteDirectory);
        }///<summary>/// Elimina un archivo en el host remoto.///</summary>///<param name="remoteFile">El archivo remoto a eliminar.</param>publicoverridevoid DeleteFile(string remoteFile)
        {
            RunCommand(@"rm " + remoteFile);
        }///<summary>/// Mueve un archivo, posiblemente cambiando el nombre en el host remoto.///</summary>///<param name="currentRemoteFile">El archivo a mover.</param>///<param name="newRemoteFile">La nueva ubicaci�n y nombre.</param>publicoverridevoid MoveFile(string currentRemoteFile, string newRemoteFile)
        {
            RunCommand(@"mv " + currentRemoteFile + " " + newRemoteFile);
        }///<summary>/// Determina si existe el archivo en el host remoto.///</summary>///<param name="remoteFile">El archivo remoto.</param>///<returns>///<c>true</c> si existe, <c>false</c> caso contrario.///</returns>publicoverridebool ExistFile(string remoteFile)
        {try
            {
                RunCommand(@"ls " + remoteFile);returntrue;
            }catch (FileTransferException)
            {returnfalse;
            }
        }///<summary>/// Determina si existe el directorio en el host remoto.///</summary>///<param name="remoteDirectory">El directorio remoto.</param>///<returns>///<c>true</c> si existe, <c>false</c> caso contrario.///</returns>publicoverridebool ExistDirectory(string remoteDirectory)
        {try
            {
                RunCommand(@"cd " + remoteDirectory);returntrue;
            }catch (FileTransferException)
            {returnfalse;
            }
        }///<summary>/// Obtiene un listado de archivos del directorio remoto.///</summary>///<param name="remoteDirectory">El directorio remoto a consultar.</param>///<returns>/// Un listado de archivos.///</returns>///<exception cref="IT.FileTransfer.FileTransferException"></exception>publicoverridestring[] ListDirectory(string remoteDirectory)
        {using (SshClient client = new SshClient(Host, User, Password))
            {
                client.Connect();using (SshCommand cmd = client.RunCommand("ls -l " + remoteDirectory))
                {if (cmd.ExitStatus != 0)thrownew FileTransferException(cmd.Error)
                        {
                            RemotePath = remoteDirectory
                        };return cmd.Result.Split(newstring[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
                }
            }
        }


Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>