在 C# 中使用 WinSCP 使用 TreeView 显示 SFTP 目录结构
Showing SFTP directory structure with TreeView using WinSCP in C#
我正在努力实现以下目标,
使用 WinSCP .NET 程序集构建一个 TreeView
组件显示 SFTP 文件夹的所有目录和子目录 API.
像这样:
Home ---->SubFolder
|
|---->Another Folder -------> MyFolder1
| | -------> MyFolder2
|
|---->MyOtherFolder
我正在遵循这种方法,但不知何故对于 WinSCP RemoteDirectoryInfo
它不起作用。
private void ListDirectory(TreeView treeView, string path)
{
treeView.Nodes.Clear();
var rootDirectoryInfo = new DirectoryInfo(path);
treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
}
private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
directoryNode.Nodes.Add(CreateDirectoryNode(directory));
foreach (var file in directoryInfo.GetFiles())
directoryNode.Nodes.Add(new TreeNode(file.Name));
return directoryNode;
}
当我按照此方法使用 WinSCP.RemoteDirectoryInfo
而不是 DirectoryInfo
时,它不起作用,因为 RemoteDirectoryInfo
没有与 IO.DirectoryInfo
相同的属性和方法。
我发现更好的方法可能是使用 EnumerateDirectories
,它将显示所有文件夹和子文件夹。
var fileInfos =
sftpSession.EnumerateRemoteFiles(
"/source", "*",
WinSCP.EnumerationOptions.AllDirectories |
WinSCP.EnumerationOptions.EnumerateDirectories);
foreach (RemoteFileInfo fi in fileInfos)
{
if (fi.IsDirectory) {
treeView1.Nodes.Add(fi.FullName);
}
}
但这会导致:
我要实现的地方:
使用 WinSCP Session.ListDirectory
获取子目录和文件列表以及特定文件夹(而不是从您的代码中获取 DirectoryInfo.GetDirectories
和 DirectoryInfo.GetFiles
)。
并使用RemoteFileInfo.IsDirectory
区分子文件夹和文件。
private void ListDirectory(TreeView treeView, string path)
{
treeView.Nodes.Clear();
treeView.Nodes.Add(CreateDirectoryNode(path));
}
private TreeNode CreateDirectoryNode(string path)
{
var directoryNode = new TreeNode(RemotePath.GetFileName(path));
var entries = sftpSession.ListDirectory(path).Files;
var directories =
files.Where(_ => _.IsDirectory &&
!_.IsParentDirectory && !_.IsThisDirectory);
foreach (var directory in directories)
directoryNode.Nodes.Add(CreateDirectoryNode(directory.FullName));
foreach (var file in entries.Where(_ => !_.IsDirectory))
directoryNode.Nodes.Add(new TreeNode(file.Name));
return directoryNode;
}
我正在努力实现以下目标,
使用 WinSCP .NET 程序集构建一个 TreeView
组件显示 SFTP 文件夹的所有目录和子目录 API.
像这样:
Home ---->SubFolder
|
|---->Another Folder -------> MyFolder1
| | -------> MyFolder2
|
|---->MyOtherFolder
我正在遵循这种方法,但不知何故对于 WinSCP RemoteDirectoryInfo
它不起作用。
private void ListDirectory(TreeView treeView, string path)
{
treeView.Nodes.Clear();
var rootDirectoryInfo = new DirectoryInfo(path);
treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
}
private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
directoryNode.Nodes.Add(CreateDirectoryNode(directory));
foreach (var file in directoryInfo.GetFiles())
directoryNode.Nodes.Add(new TreeNode(file.Name));
return directoryNode;
}
当我按照此方法使用 WinSCP.RemoteDirectoryInfo
而不是 DirectoryInfo
时,它不起作用,因为 RemoteDirectoryInfo
没有与 IO.DirectoryInfo
相同的属性和方法。
我发现更好的方法可能是使用 EnumerateDirectories
,它将显示所有文件夹和子文件夹。
var fileInfos =
sftpSession.EnumerateRemoteFiles(
"/source", "*",
WinSCP.EnumerationOptions.AllDirectories |
WinSCP.EnumerationOptions.EnumerateDirectories);
foreach (RemoteFileInfo fi in fileInfos)
{
if (fi.IsDirectory) {
treeView1.Nodes.Add(fi.FullName);
}
}
但这会导致:
我要实现的地方:
使用 WinSCP Session.ListDirectory
获取子目录和文件列表以及特定文件夹(而不是从您的代码中获取 DirectoryInfo.GetDirectories
和 DirectoryInfo.GetFiles
)。
并使用RemoteFileInfo.IsDirectory
区分子文件夹和文件。
private void ListDirectory(TreeView treeView, string path)
{
treeView.Nodes.Clear();
treeView.Nodes.Add(CreateDirectoryNode(path));
}
private TreeNode CreateDirectoryNode(string path)
{
var directoryNode = new TreeNode(RemotePath.GetFileName(path));
var entries = sftpSession.ListDirectory(path).Files;
var directories =
files.Where(_ => _.IsDirectory &&
!_.IsParentDirectory && !_.IsThisDirectory);
foreach (var directory in directories)
directoryNode.Nodes.Add(CreateDirectoryNode(directory.FullName));
foreach (var file in entries.Where(_ => !_.IsDirectory))
directoryNode.Nodes.Add(new TreeNode(file.Name));
return directoryNode;
}