2009-07-22 224 views
0

是否有任何允許我通過SFTP刪除文件的C#開放源代碼組件?通過SFTP刪除文件

+0

只是檢查:通過SFTP,你的意思是SSH文件傳輸協議?因爲那個與FTP無關,但你的問題是用FTP標記的。 – Thorarin 2009-07-22 18:43:36

+0

我不同意,SFTP和FTP在某些方面是相似的。 – djangofan 2009-07-22 18:48:18

+0

@djangofan概念是相似的,但實現完全不同。 SFTP協議與FTP沒有任何共同之處(名稱除外)。有關FTP,FTPS,FTP/SSL和SFTP之間的區別,請參閱http://www.rebex.net/secure-ftp.net/。 – 2009-10-27 13:02:27

回答

0

您可以使用OpenSSH併發出sftp批處理命令。所有你需要在c#端做的事情是用正確的命令行啓動sftp進程。

1

Tamir Gal的Sharp SSH是SFTP for .NET的開源實現。試一試。

如果您提供完全支持的商業組件,可以試試我們的Rebex SFTP。以下代碼ilustrates概念:

using Rebex.Net; 

// create client and connect 
Sftp client = new Sftp(); 
client.Connect(hostname); 
client.Login(username, password); 

// delete the file 
client.DeleteFile("/path/to/the/file"); 

// disconnect 
client.Disconnect(); 
0

我一直在使用http://sshnet.codeplex.com/。它對我來說效果很好,並且正在積極開發/支持。

要刪除的文件的代碼是一樣簡單

public static void DownloadFile(SftpClient client, SftpFile remoteFileName) 
{ 
    var localFileName = System.IO.Path.GetFileName(remoteFileName.Name); 
    using (var file = File.OpenWrite(localFileName)) 
    { 
     client.DownloadFile(remoteFileName.FullName , file); 
     remoteFileName.Delete(); 
    } 
} 
0

執行Linux命令RM與對象SshExec。這個命令刪除文件。 例子:

RM /dir1/dir2/file.txt

其他實例Tamir Execute Command

public static bool DeleteFile(string remotePath) 
{ 
    try 
    { 
     SshExec comando = new SshExec(Server, User); 
     comando.Password = Password; 

     comando.Connect(); 

     string paso = comando.RunCommand("rm " + remotePath); 

     comando.Close(); 

     return true; 
    } 
    catch (Exception ex) 
    { 

     mErrorSFTP = ex.Message; 
     return false; 
    } 
}