![](/img/trans.png)
[英]How to remove “.” and “..” files from remote directory with C# and WinSCP
[英]How could I add nodes of a treeview as folders from a remote directory with WinSCP and C#
我正在嘗試創建一個樹視圖來使用 FTP/SFTP 連接在遠程服務器中搜索目錄,我想要做的是開始用從主目錄開始的所有可用目錄填充樹視圖,例如下面的例子:
Home---->SubFolder
|
|---->Another Folder
|
|---->MyOtherFolder
然后,當用戶開始單擊每個文件夾時,它開始從樹視圖中顯示其子目錄作為以下示例(單擊另一個文件夾):
Home ---->SubFolder
|
|---->Another Folder -------> MyFolder1
| | -------> MyFolder2
|
|---->MyOtherFolder
我正在嘗試獲取這些文件夾,但它拋出了一個異常,它也在收集文件,而不是文件夾....
這是我擁有的代碼....
private void FillTree()
{
SessionOptions SessionOptions = new SessionOptions();
Session MySession = new Session();
SessionOptions.HostName = InterfaceValues[0];
SessionOptions.UserName = InterfaceValues[2];
SessionOptions.Password = InterfaceValues[3];
SessionOptions.PortNumber = Convert.ToInt32(InterfaceValues[1]);
if (string.Compare(InterfaceValues[9], "FTP", true) == 0)
SessionOptions.Protocol = WinSCP.Protocol.Ftp;
else if (string.Compare(InterfaceValues[9], "SFTP", true) == 0)
{
SessionOptions.Protocol = WinSCP.Protocol.Sftp;
SessionOptions.SshPrivateKeyPath = InterfaceValues[12];
SessionOptions.SshHostKeyFingerprint = InterfaceValues[10];
}
try
{
MySession.Open(SessionOptions);
foreach (RemoteFileInfo info in MySession.EnumerateRemoteFiles("/", "*", EnumerationOptions.AllDirectories))
{
if (info.IsDirectory)
tvRemoteDirectory.Nodes.Add(info.Name);
}
MySession.Close();
}
catch (Exception ex)
{
MySession.Close();
MessageBox.Show("Not possible to connect to " + InterfaceValues[0] + "\nError Message: " + ex.Message);
this.Close();
}
我得到的例外是:
{WinSCP.SessionRemoteException: Error listing directory '/jpm_icl'. ---> WinSCP.SessionRemoteException: Permission denied. Error code: 3 Error message from server: Permission Denied!
知道此時我能做什么嗎?
我所做的是:
ListDirectory函數檢索所有目錄,因為我不希望目錄“。” 和“。” 我必須排除它。
RemoteDirectoryInfo RemoteDirectory;
if (RemoteDirectoryPath != "Home")
RemoteDirectory = MySession.ListDirectory(RemoteDirectoryPath);
else
RemoteDirectory = MySession.ListDirectory("/");
if (tvRemoteDirectory.SelectedNode.Nodes.Count > 0) tvRemoteDirectory.SelectedNode.Nodes.Clear();
foreach (RemoteFileInfo fileinfo in RemoteDirectory.Files)
{
if (fileinfo.IsDirectory)
{
if (fileinfo.Name != "." &&
fileinfo.Name != "..")
{
TreeNode ChildNode = new TreeNode();
ChildNode.Text = fileinfo.Name;
ChildNode.ImageIndex = 0;
tvRemoteDirectory.SelectedNode.Nodes.Add(ChildNode);
tvRemoteDirectory.ExpandAll();
}
}
}
使用RadFileExplorer-Telerik ASP.NET FileExplorer
並且此功能將滿足您的要求FileExplorer-在TreeView中顯示所有項目
其他所有關鍵功能均在下方列出,您需要
您可以從Telerik獲得適用於任何平台的此功能。
集成在Telerik.Web.UI中的單個控件-准備在頁面上拖放
使用ASP.NET 2.0 AJAX回調機制按需加載目錄負載
用於文件操作的客戶端和服務器事件
使用RadEditor的FileBrowserContentProvider抽象來掛鈎到任何基礎文件系統,例如OS,數據庫,MOSS SharePoint,MCMS
文件和文件夾的排序
上下文菜單
能夠刪除和重命名文件和文件夾
能夠創建新文件夾
如果文件夾包含大量項目(例如,超過200個),則能夠啟用網格的分頁
使用新的工具欄按鈕可以在網格視圖和縮略圖視圖之間快速切換
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.