简体   繁体   English

如何使用 SharpSsh 和 C# 将文件从一个文件夹移动到远程服务器上的另一个文件夹

[英]How to move a file from one folder to another folder on a remote server using SharpSsh and C#

How to move a file from one folder to another folder on a remote server using SharpSsh?如何使用 SharpSsh 将文件从一个文件夹移动到远程服务器上的另一个文件夹?

I'm trying to move a file that is in a folder on the server to another server folder.我正在尝试将服务器文件夹中的文件移动到另一个服务器文件夹。

I'm getting:我越来越:

ERROR: No se pudo encontrar el archivo '/local/opt/oracle/oradata/UTL_DIR/PEDIMENTOS/pedimento.csv'.错误:没有 se pudo encontrar el archivo '/local/opt/oracle/oradata/UTL_DIR/PEDIMENTOS/pedimento.csv'。

Cannot find file '/local/opt/oracle/oradata/UTL_DIR/PEDIMENTOS/pedimento.csv'找不到文件“/local/opt/oracle/oradata/UTL_DIR/PEDIMENTOS/pedimento.csv”

This is my code:这是我的代码:

Tamir.SharpSsh.Sftp ClientSFTP = new Tamir.SharpSsh.Sftp(pHost, pUserName, pPassword);
try
{
    string FechaActual = DateTime.Today.ToString("yyyyMMdd");
    string pFilePEDIMENTOS = "/local/opt/oracle/oradata/UTL_DIR/PEDIMENTOS/pedimento.csv";
    string pFilePROCESADO = "/local/opt/oracle/oradata/UTL_DIR/PEDIMENTOS/PROCESADO/pedimento" + FechaActual + ".csv";

    //Abre sesion
    ClientSFTP.Connect();
   
    if (ClientSFTP.Connected)
    {
        // ejecutar el comando
        ClientSFTP.Put(pFilePEDIMENTOS, pFilePROCESADO);//SEGUIR INVESTIGANDO
    }
    else
    {
        throw new Exception("Error de Conexion con el Servidor Remoto");
    }
}
catch (Exception ex)
{
    lblError.Text = ex.Message;
}
finally
{
    //cerrar conexion SFTP
    ClientSFTP.Close();
}

I assume that Put method which you use is to transfer the file from the local computer (the client) to the remote server.我假设您使用的 Put 方法是将文件从本地计算机(客户端)传输到远程服务器。 The method is not suitable for moving files on the remote server.该方法不适用于在远程服务器上移动文件。

To move files you need to either use the SSH client and issue commands for remote shell to interpret (if you know it's linux, then running "mv" in shell will do the job), or, if you have no SSH access, you can try using a decent SFTP client (such as our SecureBlackbox) which supports "Rename" SFTP command (remote file can be "renamed" this way).要移动文件,您需要使用 SSH 客户端并向远程 shell 发出命令以进行解释(如果您知道它是 linux,则在 shell 中运行“mv”即可完成这项工作),或者,如果您没有 SSH 访问权限,则可以尝试使用支持“重命名”SFTP 命令的体面的 SFTP 客户端(例如我们的 SecureBlackbox)(远程文件可以通过这种方式“重命名”)。 Not all servers support this command, though.不过,并非所有服务器都支持此命令。

First, do not use the SharpSSH, it's a dead project.首先,不要使用 SharpSSH,它是一个死项目。

Use another SFTP implementation.使用另一个 SFTP 实现。 See SFTP Libraries for .NET .请参阅.NET 的 SFTP 库


Anyway, if you have to use it (for a very good reason), use SftpChannel.rename method.无论如何,如果您必须使用它(有一个很好的理由),请使用SftpChannel.rename方法。

You cannot use the Sftp class, as it does not expose the method.您不能使用Sftp类,因为它不公开该方法。

See jsch\\examples\\Sftp.cs example.参见jsch\\examples\\Sftp.cs示例。 A simplified code is like:一个简化的代码是这样的:

Session session=jsch.getSession(pUserName, pHost, 22);
...
session.connect();
...
Channel channel=session.openChannel("sftp");
ChannelSftp c=(ChannelSftp)channel;
...
c.rename(pFilePEDIMENTOS, pFilePROCESADO);

The "rename" or "move" are basically the same operations. “重命名”或“移动”基本上是相同的操作。 The .Put is for uploading a local file to a remote location. .Put用于将本地文件上传到远程位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用SharpSSH将文件移动到SFTP服务器 - How to Move a file to SFTP server using SharpSSH 如何在C#中将文件从一个文件夹复制到另一个文件夹? - How to copy file from one folder to another in C#? 将文件从一个文件夹移动到另一进程正在使用的C#错误文件 - Move files from one folder to another C# Error File being used by another process 尝试使用子文件夹C#将文件从一个文件夹移动到另一个文件夹 - Trying to move files from one folder to another with subfolders C# 使用 web 服务将文件从一个服务器文件夹复制到另一个文件夹 - Copy a file from one server folder to another folder using webservice 如何将文件移动到共享目录中的C#中的另一个文件夹 - How to move a file exists into shared directory to another folder in c# 将文件移动到另一个目录(特殊文件夹中的文件夹)C# - Move a file to another directory (folder within a special folder) C# 如何从远程服务器文件夹中访问所有文件名-C#? - how to access all filenames from a remote server folder - c#? 使用c#仅从文件夹中移动第一个.txt文件 - Move only the first .txt file from a folder using c# 如何从C#中的远程服务器从特定文件夹中获取文件列表? - How to get file list from specific folder from remote server in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM