Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
Graph .NET
>
Tutorial
> Download file as stream
Download file as stream.
C# example
using System; using System.IO; using System.Threading.Tasks; using Independentsoft.Graph; using Independentsoft.Graph.Files; namespace ConsoleApp1 { class Program { static async Task Main() { try { GraphClient client = new GraphClient(); client.ClientId = "63333333-209e-454e-b7bd-55a4d201270f"; client.Tenant = "independentsoft.onmicrosoft.com"; client.ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; Stream inputStream = await client.GetDriveItemAsStream("/Folder1/Test.pdf", new DriveId("b!T7ZW_FMzJ02x_G6HteZHZ")); FileStream outputStream = new FileStream("e:\\Test-download.pdf", FileMode.CreateNew); using (inputStream) { using (outputStream) { byte[] buffer = new byte[1024]; int bytesRead = inputStream.Read(buffer, 0, buffer.Length); while (bytesRead > 0) { outputStream.Write(buffer, 0, bytesRead); bytesRead = inputStream.Read(buffer, 0, buffer.Length); } } } } catch (GraphException ex) { Console.WriteLine("Error: " + ex.Code); Console.WriteLine("Message: " + ex.Message); Console.Read(); } } } }
VB example
Imports System.IO Imports Independentsoft.Graph Imports Independentsoft.Graph.Files Namespace Sample Class Module1 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 Dim client As GraphClient = New GraphClient() client.ClientId = "63333333-209e-454e-b7bd-55a4d201270f" client.Tenant = "independentsoft.onmicrosoft.com" client.ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" Dim inputStream As Stream = Await client.GetDriveItemAsStream("/Folder1/Test.pdf", New DriveId("b!T7ZW_FMzJ02x_G6HteZHZ")) Dim outputStream As FileStream = New FileStream("e:\Test-download.pdf", FileMode.CreateNew) Using inputStream Using outputStream Dim buffer As Byte() = New Byte(1023) {} Dim bytesRead As Integer = inputStream.Read(buffer, 0, buffer.Length) While bytesRead > 0 outputStream.Write(buffer, 0, bytesRead) bytesRead = inputStream.Read(buffer, 0, buffer.Length) End While End Using End Using Catch ex As GraphException Console.WriteLine("Error: " + ex.Code) Console.WriteLine("Error: " + ex.Message) Console.Read() End Try End Sub End Class End Namespace
Need help? Ask our developers:
Name*
Email*
Message*