我正在构建一个VB.NET Google Drive文件浏览器应用程序,原因可能是您不关心。我正在使用谷歌驱动器v2 sdk,我正在尝试使用用户的整个Google Drive目录结构填充树视图控件(不是文件,只是文件夹。)我的代码工作正常,但是,我担心,它对于拥有大量嵌套文件夹的用户,需要几分钟时间。我使用后台工作线程使用委托函数填充树视图。任何人都可以建议一种更有效的方法吗?
从我的代码中调用NewPopulate()来创建后台工作线程。从这里开始:
Private Sub NewPopulate()
TreeView1.Nodes.Add(ActiveUser.Email, ActiveUser.Email)
bwPopulateTree = New BackgroundWorker
AddHandler bwPopulateTree.DoWork, AddressOf PopulateTreeStart
AddHandler bwPopulateTree.RunWorkerCompleted, AddressOf PopulateTreeFinished
bwPopulateTree.RunWorkerAsync()
cmbActiveUsers.Enabled = False
End Sub
Private Sub PopulateTreeStart()
Dim children As ChildList = FileManager.GetFoldersInFolder(ActiveUser.DriveService, "root")
PopulateTreeLoop(ActiveUser.Email, children)
End Sub
Private Sub PopulateTreeFinished()
lblToolStripStatus.Text = "Directory listing completed."
SaveTree()
cmbActiveUsers.Enabled = True
End Sub
Private Sub PopulateTreeLoop(nodeKey As String, ByVal childList As ChildList)
If Not childList Is Nothing Then
Dim user As GoogleUser = ActiveUser
For Each child As ChildReference In childList.Items
Dim successful As Boolean = False
If TreeView1.InvokeRequired Then
successful = TreeView1.Invoke(New m_FindAddNode(AddressOf FindAddNode), {nodeKey, child})
Else
successful = FindAddNode(nodeKey, child)
End If
Dim children As ChildList = FileManager.GetFoldersInFolder(user.DriveService, child.Id)
If Not children Is Nothing Then
PopulateTreeLoop(child.Id, children)
End If
Next
End If
End Sub
Private Function FindAddNode(nodeKey As String, child As ChildReference) As Boolean
'Returns true if successful
Try
Dim service As DriveService = ActiveUser.DriveService
Dim file As Drive.v2.Data.File = service.Files.Get(child.Id).Fetch()
Dim node() As TreeNode = TreeView1.Nodes.Find(nodeKey, True)
Dim strTitle As String
If file.FileExtension Is Nothing Then
strTitle = file.Title
Else
strTitle = If(file.Title.EndsWith(file.FileExtension), file.Title, file.Title + file.FileExtension)
End If
node(0).Nodes.Add(file.Id, strTitle)
Return True
Catch ex As Exception
Return False
End Try
End Function
这是我的FileManager类函数:
Public Shared Function GetFoldersInFolder(service As DriveService, ByVal folderId As String) As ChildList
Dim request As Google.Apis.Drive.v2.ChildrenResource.ListRequest = service.Children.List(folderId)
request.Q = "mimeType='application/vnd.google-apps.folder'"
Return request.Fetch()
End Function
抱歉,我必须包含这么多代码,但我试图只包含必要的代码。我也已经有了允许我缓存目录结构的代码,但决定它很难实现,因为我必须检查以确保事情没有改变......我真的只需要更快的检索方式目录结构。我知道另一种方法是让treeview只在用户点击父文件夹时填充子文件夹,但我想避免等待服务器响应每个请求所产生的短暂停顿,每次用户点击新文件夹。
我还有一个检索所有文件夹的功能:
Public Shared Function GetAllFolders(service As DriveService) As FileList
Dim request As Google.Apis.Drive.v2.FilesResource.ListRequest = service.Files.List
Dim request2 As Google.Apis.Drive.v2.FilesResource.ListRequest = service.Files.List
request.Q = "mimeType='application/vnd.google-apps.folder'"
Return request.Fetch()
End Function
但我无法想出任何有效的方法来解析该列表以提出目录结构......任何想法?我非常感谢你的帮助。我已经做了好几天......
答案 0 :(得分:0)
你的方法很合理。很抱歉,使用大型Google Drives需要很长时间。我当然认为只有在用户尝试扩展父节点时才填充子节点,这是正确的想法。预期暂停,但你可以通过向用户显示一些反馈来减轻它,例如Loading...
。
在这个云计算时代,用户在从远程服务加载数据时会产生一些延迟。随着网络速度的提高,这将在未来得到改善。