我正在使用Sharefile API来管理我的项目中的文件和文件夹。我需要从root用户获取所有文件夹和子文件夹。这是列出指定为参数的路径中包含的文件夹的函数:
Public Function f_FolderList(ByVal path As String) As List(Of String)
Dim result As New List(Of String)
Dim requiredParameters As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
requiredParameters.Add("path", path)
Dim url As String = BuildUrl("folder", "list", requiredParameters)
Dim jsonObj As JObject = InvokeShareFileOperation(url)
Dim errorStatus As Boolean = jsonObj.GetValue("error")
If Not errorStatus Then
Dim items As JArray = jsonObj.GetValue("value")
For Each item As JObject In items
result.Add(item.GetValue("filename"))
Next
Return result
Else
'MsgBox("nothing happened")
End If
Return result
End Function
在这里,我尝试获取所有文件夹(目前只在msgbox中显示结果):
使用此代码(现在在编辑之后),我在根目录下实现了1级和2级文件夹,我可以永远编写列表和子列表,但这是不可行的。我需要检查子文件夹是否存在的内容,以便创建或不存在子列表。
有人可以帮忙吗?
(编辑) 这里有一些我做的改变,虽然没有得到预期的结果但是更接近(我认为)。
Private Sub btn_FoldersTree_Click(sender As Object, e As EventArgs) Handles btn_FoldersTree.Click
tv_Folders.Nodes.Clear()
Dim folderList As List(Of String) = sfs.f_FolderList(rootPath)
Dim path As String = rootPath
Dim count As Integer = folderList.Count()
Dim tempPath As String = String.Empty
For Each folder As String In folderList
path += "/" + folder
MsgBox(path)
folderList = sfs.f_FolderList(path)
For Each subfolder In folderList
tempPath = path
path += "/" + subfolder
MsgBox(path)
folderList = sfs.f_FolderList(path)
path = tempPath
Next
path = rootPath
Next
End Sub
答案 0 :(得分:2)
尝试使用递归函数,如下所示:从rootPath
开始,例如“E:\ Program Files \ Fiddler2”,将找到anysubfolder并列在{{1 }}
listDir
E.g。的结果强>
Private Sub btn_FoldersTree_Click(sender As Object, e As EventArgs) Handles btn_FoldersTree.Click
Dim listDir As New List(Of String) '= sfs.f_FolderList(rootPath)
Dim path As String = rootPath
GetDirectories(path, listDir)
'For Each s As String In listDir
'MsgBox(s)
'Next
End Sub
Sub GetDirectories(ByVal rootPath As String, ByRef DirectoryList As List(Of String))
Try
Dim dirArray() As String = Directory.GetDirectories(rootPath)
DirectoryList.AddRange(dirArray)
'Get total number of subdirs
Console.WriteLine(String.Format("Dir: {0}, SubDirCount: {1}", rootPath, dirArray.Count()))
For Each Dir As String In dirArray
GetDirectories(Dir, DirectoryList)
Next
Catch ex As Exception
End Try
End Sub
主要编辑
适应真实的sharefile帐户和结构:
Video: Sharefile trial account(now deleted) api project built starting from previous link