[英]How could I add nodes of a treeview as folders from a remote directory with WinSCP and C#
I'm trying to create a tree view to search for a directories in a remote server using FTP/SFTP connections, What I'm trying to do is start filling the tree view with all the available directories starting with the home directory such as the following example:我正在尝试创建一个树视图来使用 FTP/SFTP 连接在远程服务器中搜索目录,我想要做的是开始用从主目录开始的所有可用目录填充树视图,例如下面的例子:
Home---->SubFolder
|
|---->Another Folder
|
|---->MyOtherFolder
Then when the user start clicking in each folder it start to display their subdirectories from the tree view as the follwoing example (clicking in Another Folder):然后,当用户开始单击每个文件夹时,它开始从树视图中显示其子目录作为以下示例(单击另一个文件夹):
Home ---->SubFolder
|
|---->Another Folder -------> MyFolder1
| | -------> MyFolder2
|
|---->MyOtherFolder
I'm trying to get those folders but it's throwing an exception, also it is gathering files, not folders....我正在尝试获取这些文件夹,但它抛出了一个异常,它也在收集文件,而不是文件夹....
this is the code that I have....这是我拥有的代码....
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();
}
The exception that I'm getting is:我得到的例外是:
{WinSCP.SessionRemoteException: Error listing directory '/jpm_icl'. ---> WinSCP.SessionRemoteException: Permission denied. Error code: 3 Error message from server: Permission Denied!
Any idea what could I do at this point?知道此时我能做什么吗?
What I did was this: 我所做的是:
ListDirectory function to retrieve all the directories, as I don't want the directory "." ListDirectory函数检索所有目录,因为我不希望目录“。” and "." 和“。” I have to exclude it. 我必须排除它。
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();
}
}
}
Use RadFileExplorer - Telerik ASP.NET FileExplorer 使用RadFileExplorer-Telerik ASP.NET FileExplorer
And this feature will complete your requirements FileExplorer - Show all items in the TreeView 并且此功能将满足您的要求FileExplorer-在TreeView中显示所有项目
All other Key Features are given below you would like it 其他所有关键功能均在下方列出,您需要
From Telerik you can get this feature for any platform. 您可以从Telerik获得适用于任何平台的此功能。
A single control, integrated in Telerik.Web.UI - ready to drag and drop on the page 集成在Telerik.Web.UI中的单个控件-准备在页面上拖放
Directory load on demand loading using ASP.NET 2.0 AJAX Callback mechanism 使用ASP.NET 2.0 AJAX回调机制按需加载目录负载
Clientside and server events for file operations 用于文件操作的客户端和服务器事件
Uses the FileBrowserContentProvider abstraction of RadEditor for hooking to any underlying file system, such as OS, database, MOSS SharePoint, MCMS 使用RadEditor的FileBrowserContentProvider抽象来挂钩到任何基础文件系统,例如OS,数据库,MOSS SharePoint,MCMS
Sorting of files and folders 文件和文件夹的排序
Context menus 上下文菜单
Ability to delete and rename files and folders 能够删除和重命名文件和文件夹
Ability to create new folders 能够创建新文件夹
Ability to enable paging for the grid if the folders contain a large number of items (eg more than 200) 如果文件夹包含大量项目(例如,超过200个),则能够启用网格的分页
Ability to switch quickly between Grid and Thumbnail views using the new toolbar buttons 使用新的工具栏按钮可以在网格视图和缩略图视图之间快速切换
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.