Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
Graph .NET
>
Tutorial
> List entire drive
List entire drive recursively.
C# example
using System; using System.Collections.Generic; using System.Threading.Tasks; using Independentsoft.Graph; using Independentsoft.Graph.Files; namespace ConsoleApp1 { class Program { static GraphClient client = null; static IList
allItems = new List
(); static async Task Main() { try { client = new GraphClient(); client.ClientId = "63333333-209e-454e-b7bd-55a4d201270f"; client.Tenant = "independentsoft.onmicrosoft.com"; client.ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; await ListFolder("/", new DriveId("b!T7ZW_FMzJ02x_G6HteZHZ")); foreach (DriveItem item in allItems) { Console.WriteLine("Id: " + item.Id); Console.WriteLine("Name: " + item.Name); Console.WriteLine("WebUrl: " + item.WebUrl); if (item.FolderProperties != null) { Console.WriteLine("Folder: true"); } Console.WriteLine("-----------------------------------------------------------------"); } } catch (GraphException ex) { Console.WriteLine("Error: " + ex.Code); Console.WriteLine("Message: " + ex.Message); Console.Read(); } } private static async Task ListFolder(string id, DriveId driveId) { IList
subFolders = new List
(); GetDriveItemsResponse response = await client.GetDriveItems(id, driveId); while (true) { foreach (DriveItem item in response.DriveItems) { allItems.Add(item); if (item.FolderProperties != null) { subFolders.Add(item); } } if(response.NextLink == null) { break; } else { response = await client.GetDriveItems(response.NextLink); } } foreach (DriveItem item in subFolders) { await ListFolder(item.Id, driveId); } } } }
VB example
Imports System Imports System.Threading.Tasks Imports Independentsoft.Graph Imports Independentsoft.Graph.Files Namespace Sample Class Module1 Shared client As GraphClient = Nothing Shared allItems As IList(Of DriveItem) = New List(Of DriveItem)() Shared Sub Main(ByVal args As String()) Dim task = New Task(AddressOf Run) ' Start and wait for task to end. task.Start() Console.Read() End Sub Shared Async Sub Run() Try client = New GraphClient() client.ClientId = "63333333-209e-454e-b7bd-55a4d201270f" client.Tenant = "independentsoft.onmicrosoft.com" client.ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" Await ListFolder("/", New DriveId("b!T7ZW_FMzJ02x_G6HteZHZ")) For Each item As DriveItem In allItems Console.WriteLine("Id: " & item.Id) Console.WriteLine("Name: " & item.Name) Console.WriteLine("WebUrl: " & item.WebUrl) If item.FolderProperties IsNot Nothing Then Console.WriteLine("Folder: true") End If Console.WriteLine("-----------------------------------------------------------------") Next Catch ex As GraphException Console.WriteLine("Error: " + ex.Code) Console.WriteLine("Error: " + ex.Message) Console.Read() End Try End Sub Private Shared Async Function ListFolder(ByVal id As String, ByVal driveId As DriveId) As Task Dim subFolders As IList(Of DriveItem) = New List(Of DriveItem)() Dim response As GetDriveItemsResponse = Await client.GetDriveItems(id, driveId) While True For Each item As DriveItem In response.DriveItems allItems.Add(item) If item.FolderProperties IsNot Nothing Then subFolders.Add(item) End If Next If response.NextLink Is Nothing Then Exit While Else response = Await client.GetDriveItems(response.NextLink) End If End While For Each item As DriveItem In subFolders Await ListFolder(item.Id, driveId) Next End Function End Class End Namespace
Need help? Ask our developers:
Name*
Email*
Message*