0

I want to make an application which can view files directly from the client desktop in the same hierarchy as of his/her google drive storage. But for that I have to get the folder hierarchy for that particular user account. In my application I am trying to view the folder hierarchy in a treeview. Below is the code:

internal async Task<TreeView> SetTreeView(TreeNode node, string fileId, TreeView treeview1)
    {
        TreeNode t;
        if (node == null)
        {
            t = new TreeNode();
        }
        else
            t = node;

        FilesResource.GetRequest req = driveservice.Files.Get(fileId);
        var root = req.Execute();
        FilesResource.GetRequest get;
            await DriveV2.Authorize();
            var childrenIds = DriveV2.GetChildren(root.Id);


        for ( int i = 0; i < childrenIds.Count; i++)
            {
                get = driveservice.Files.Get(childrenIds[i]);
                var file = get.Execute();
            var n = t.Nodes.Add(file.Name);

            SetTreeView(n,childrenIds[i], treeview1);
            }
        treeview1.Nodes.Add(t);
        //doc.Save("file.xml");

        return treeview1;
    }

Please correct me if I am wrong at any point and suggest some other implementations by which I can get the hierarchy either in a XML file(storing the IDs of folders) or directly into a treeview.

Edit: The code I've written does adds the folders into the treeview but all at the same level and not in a hierarchical format.

HarsiddhDave
  • 91
  • 2
  • 9
  • What is wrong with this code? Please narrow your question down...what are you looking for? – rory.ap Dec 23 '16 at 17:07
  • @rory.ap Actually it does adds the folders to treeview but not in a hierarchical format. It just displays all the folders in a single level. – HarsiddhDave Dec 23 '16 at 17:09
  • 1
    This might be already answered at http://stackoverflow.com/questions/6239544/populate-treeview-with-file-system-directory-structure – ivayle Dec 23 '16 at 18:54
  • @IvayloPetrov It worked for me. Although I had to change some statements to make it work with the Google Drive API. – HarsiddhDave Dec 24 '16 at 14:14
  • Despite the fact that the referenced question has an accepted answer, that's not the best answer for Google Drive. Making recursive calls to get successive folder contents is very time consuming. A better approach is to for a file.list to query ALL folders, fetching the ID, name/title, and parents. Once you have all of the folders you can build up the hierarchy in memory. Also bear in mind that nowhere does it say that Drive folders form a strict hierarchy. It's possible to have folder-a be a parent of folder-b which is a parent of folder-a. – pinoyyid Dec 24 '16 at 14:29
  • You might wanna check out [this](http://stackoverflow.com/questions/36052163/get-folder-hierarchy-with-google-drive-api-c-net/41410375#41410375) answer.. – Divins Mathew Jan 02 '17 at 16:04

0 Answers0