【发布时间】:2016-08-05 05:47:46
【问题描述】:
我正在尝试创建一个树视图以使用 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!
知道此时我能做什么吗?
【问题讨论】:
-
要么获取权限,要么忽略错误。
-
我试过这个:
RemoteDirectoryInfo RemoteDirectory = MySession.ListDirectory("/"); foreach (RemoteFileInfo fileinfo in RemoteDirectory.Files) { //tvRemoteDirectory.Nodes.Add(fileinfo.Name); }但它正在检索“。”和“..”和“jpm_icl”,但我不知道“jpm_icl”怎么样,我看不到那个文件夹 -
什么时候得到异常?在
EnumerateRemoteFiles循环中?哪里看不到jpm_icl文件夹? -
嗨@MartinPrikryl,是的,在循环中,第三次抛出异常......我的意思是,远程服务器中不存在
jmp_icl,我不确定是否是一个隐藏文件夹。 -
我已经在windows server中检查了文件夹和“显示隐藏文件和文件夹”选项并打开,不知道
jmp_icl是什么
标签: c# .net treeview directory-structure winscp