开发者

can not rename remote file using sharpssh

i wrote like the following code. but this code is not available...

when i execute, occur. if anyone knows something, give me advice pls... thanks in advance.


void sftp_OnTransferE开发者_开发百科nd(string src, string dst, int transferredBytes
                          , int totalBytes, string message)
{

    if (sftp == null)
{
sftp = new Sftp(Const.SFTP_HOST, Const.SFTP_USER, Const.SFTP_PASSWORD);

sftp.Connect();

}

SftpChannel.rename("file/123_Uploading.zip", "file/123_Finished.zip");
}


--------------------------------------------
Sftp.cs

public void Rename(string oldPath, string newPath)
{

   SftpChannel.rename(oldPath, newPath);

}

---------------------------------------------

Error occur the following place...

---------------------------------------------------------
ChannelSftp.cs

public void rename(String oldpath, String newpath)
{

・

・

・


int i=buf.getInt(); << i == 4

if(i==SSH_FX_OK) return;

throwStatusError(buf, i); << throw error

catch(Exception e)

{

if(e is SftpException) throw (SftpException)e; << thrown error (id >> 4, message >> Failure)

throw new SftpException(SSH_FX_FAILURE, "");

}

}


The current NuGet package is still of version 1.1.1.13 which does not contain the Rename method. It would be great if the package maintainer could update it.

But in the meantime if anyone else needs it with the current NuGet package, here is a Reflection-based extension method solution. Not nice, but at least I can use the package without forking/rebuilding, etc.

public static void Rename(this Sftp client, string oldName, string newName) {
  var channelProperty = client.GetType().GetProperty("SftpChannel", BindingFlags.NonPublic | BindingFlags.Instance);
  channelProperty.GetValue(_client, null).CastTo<ChannelSftp>().rename(OldName, newName);
}


I added your

public void Rename(string oldPath, string newPath)
{

   SftpChannel.rename(oldPath, newPath);

}

code into Sftp.cs class and I called it :

  var sftp = new Sftp("sftp.example.com", username, password);
  sftp.Connect(22);
  sftp.Rename(oldValue, newValue);
  sftp.Close();

then it renamed my file successfully.Thanks by the way

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜