0

由于您可能不关心的原因,我正在构建一个 VB.NET Google Drive 文件资源管理器应用程序。我正在使用 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

抱歉,我不得不包含这么多代码,但我试图只包含必要的代码。我也已经有代码可以让我缓存目录结构,但我认为这很难实现,因为我必须检查以确保事情没有改变......我真的只需要一种更快的检索方式目录结构。我知道这样做的另一种方法是让树视图仅在用户单击父文件夹后填充子文件夹,但我想避免每次等待服务器响应每个请求时的短暂暂停用户单击一个新文件夹。

我还有一个检索所有文件夹的功能:

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

但是我想不出任何有效的方法来解析该列表以提出目录结构......有什么想法吗?我真的很感激帮助。我已经为此工作了几天...

4

1 回答 1

0

Your method is reasonable. Sorry it is taking a long time with large Google Drives. I certainly think that you are on the right idea in only populating the child nodes when the user attempts to expand the parent. The pause there is expected, but you can mitigate it somewhat by displaying some feedback to the user, e.g. Loading....

In this age of cloud computing, users expect some delay when loading data from a remote service. This will improve in the future as network speeds improve.

于 2013-01-28T06:30:54.797 回答