2013-01-22 113 views
0

由於您可能不關心的原因,我正在構建VB.NET Google Drive文件資源管理器應用程序。我使用的是谷歌驅動器v2 sdk,我試圖用用戶的整個Google Drive目錄結構填充樹視圖控件(不是文件,只是文件夾)。但是,我的代碼工作正常,但是,正如我擔心的那樣對於擁有大量嵌套文件夾的用戶來說需要花費幾分鐘的時間。我正在使用後臺工作線程使用委託函數來填充樹視圖。任何人都可以提出一個更有效的方法嗎?Google Drive SDK - 使用Google Drive目錄結構填充.NET TreeView

從我的代碼調用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 

這裏是我的文件管理類函數:

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 

但我不能拿出任何有效的方式來解析名單拿出目錄結構...任何想法?我非常感謝幫助。我一直在爲此工作...

回答

0

您的方法是合理的。抱歉,Google大型驅動器需要很長時間。我當然認爲,當用戶嘗試擴展父代時,只填充子節點是正確的。期望暫停,但是可以通過向用戶顯示一些反饋來緩解它,例如, Loading...

在這個雲計算時代,用戶從遠程服務加載數據時會期待一些延遲。隨着網絡速度的提高,這將在未來得到改善。