如何使用 SharpSSH 以编程方式从 SFTP 服务器删除文件?

发布于 2024-08-27 07:00:09 字数 66 浏览 7 评论 0原文

如何使用 Tamir Gal 的 SharpSSH 从 SFTP 服务器删除文件?我已经能够完成除删除之外的其他功能。

How to delete a file from a SFTP server using Tamir Gal's SharpSSH? I have been able to accomplish other functionality but deletion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眸 2024-09-03 07:00:09

SshExec 类对我不起作用,但一点反射魔法起作用了:

var prop = sftp.GetType().GetProperty("SftpChannel", BindingFlags.NonPublic | BindingFlags.Instance);
var methodInfo = prop.GetGetMethod(true);
var sftpChannel = methodInfo.Invoke(sftp, null);
((ChannelSftp) sftpChannel).rm(ftpPath);

The SshExec class didn't work for me, but a little Reflection magic worked:

var prop = sftp.GetType().GetProperty("SftpChannel", BindingFlags.NonPublic | BindingFlags.Instance);
var methodInfo = prop.GetGetMethod(true);
var sftpChannel = methodInfo.Invoke(sftp, null);
((ChannelSftp) sftpChannel).rm(ftpPath);
溺ぐ爱和你が 2024-09-03 07:00:09

要实现此目的,您需要修改 SharpSSH 程序集以公开您所需的功能。

获取源代码并打开 $\SharpSSH-1.1.1.13.src\SharpSSH\Sftp.cs

在类末尾之前插入以下代码行:

public void Delete(string path)
{
    SftpChannel.rm(path);
}

重新编译并在项目中引用重新编译的 DLL。您现在可以删除 SFTP 服务器上的文件。

To accomplish this you will need to modify the SharpSSH assembly to expose the functionality you require.

Obtain the source code and open $\SharpSSH-1.1.1.13.src\SharpSSH\Sftp.cs

Insert the following lines of code before the end of the class:

public void Delete(string path)
{
    SftpChannel.rm(path);
}

Recompile and reference the recompiled DLL in your project. You will now be able to delete files on the SFTP server.

丢了幸福的猪 2024-09-03 07:00:09

您还可以使用 SshExec 类,然后使用“RunCommand”方法执行“rm”命令。这样您就不必重新编译和构建新的 dll。

Well you can also use SshExec class and then execute the "rm" command using "RunCommand" method. This way you wont have to recompile and build a new dll.

玩世 2024-09-03 07:00:09

使用 Tamir 的 dll 我建议使用下面的代码删除。这样,你不需要修改Tamir的dll,而下面的代码可以在你的类中编写。

string fromFile = "/a/b/MyFile.txt"
SshExec se = new SshExec(host, username, password);
se.Connect(port);
se.RunCommand("rm " + fromFile);

Using Tamir's dll I would suggest to delete using the below code. In this way, you need not modify Tamir's dll, whereas the below code can be written in your class.

string fromFile = "/a/b/MyFile.txt"
SshExec se = new SshExec(host, username, password);
se.Connect(port);
se.RunCommand("rm " + fromFile);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文