How to Upload Files to OneDrive from WinForms C# .NET 9.0 using Microsoft Graph SDK
In the previous 'fifth' post➡️DropBox File Upload, we looked at sharing files to DropBox App Folder using DropBox Api.
In this tutorial, we’ll walk through how our GSMArena scraper built with WinForms C# .NET 9.0 can upload files directly to OneDrive using the Microsoft Graph SDK. This integration is useful for cloud backups, file sharing, and ensuring scraped data is accessible from anywhere.
In this sixth installment of our GSMArena Scraper series, we’ll cover how to prepare your OneDrive account for file uploads and walk through the C# techniques we used to integrate OneDrive file upload functionality into our WinForms application using the Microsoft Graph SDK. We’ll also break down our custom OneDriveUploader.cs class to understand how it works under the hood.
1. Preparing Your OneDrive Account
Before you can upload files to OneDrive, you must set up an application in the Azure Portal to get the credentials for Microsoft Graph API.
- Sign in to your Microsoft account and go to App Registrations.
- Click New Registration.
- Enter an Application Name (e.g., GSMArenaUploaderApp).
- Set the supported account type (e.g., "Accounts in this organizational directory only").
- Redirect URI can be left blank for now or set to
https://login.microsoftonline.com/common/oauth2/nativeclient
for testing. - Click Register.
- Application (client) ID
- Directory (tenant) ID
Next, create a Client Secret:
- Go to Certificates & secrets.
- Click New client secret, add a description, set expiry, and click Add.
- Copy the secret value immediately — you’ll need it in your WinForms app.
2. Installing and Using Microsoft Graph SDK
To upload to OneDrive, we’ll use the Microsoft Graph .NET SDK, which simplifies calling OneDrive APIs.
Install Microsoft Graph SDK
Open your project in Visual Studio 2022 and install the package via NuGet:
Install-Package Microsoft.Graph
Install-Package Microsoft.Identity.Client
Authentication
Use the following code snippet to authenticate and create a GraphServiceClient
instance:
using Microsoft.Graph;
using Microsoft.Identity.Client;
var clientId = "YOUR_CLIENT_ID";
var tenantId = "YOUR_TENANT_ID";
var clientSecret = "YOUR_CLIENT_SECRET";
var confidentialClient = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithTenantId(tenantId)
.Build();
var authProvider = new ClientCredentialProvider(confidentialClient);
var graphClient = new GraphServiceClient(authProvider);
Uploading a File to OneDrive
Once authenticated, uploading is straightforward:
using (var fileStream = System.IO.File.OpenRead(@"C:\path\to\yourfile.txt"))
{
await graphClient.Me.Drive.Root.ItemWithPath("UploadedFile.txt")
.Content
.Request()
.PutAsync<DriveItem>(fileStream);
}
This code uploads yourfile.txt
to the root of the authenticated OneDrive account.
3. Integrating with GSMArena Scraper
In our GSMArena WinForms scraper, after the scraping process finishes, we automatically generate an export file (CSV or JSON). This file is then passed to the OneDrive upload method above, ensuring that every scrape session is backed up to the cloud.
Example Integration
private async Task UploadScrapedDataToOneDrive(string filePath)
{
using (var fileStream = System.IO.File.OpenRead(filePath))
{
await graphClient.Me.Drive.Root.ItemWithPath(System.IO.Path.GetFileName(filePath))
.Content
.Request()
.PutAsync<DriveItem>(fileStream);
}
}
Conclusion
By integrating Microsoft Graph SDK into your WinForms C# .NET 9.0 project, you can seamlessly upload files to OneDrive. This is especially valuable for projects like our GSMArena scraper, where data backup and cloud accessibility are crucial.
With this setup, every run of the scraper can automatically store its output on OneDrive, eliminating manual uploads and ensuring data is always safe and accessible.
📦 GSMArena Mobile Brands
GitHub Repository Showcase
This repository contains structured data for GSM Arena mobile brands, ideal for apps and web scrapers. Clean, JSON-formatted brand lists ready to use in your projects.
⭐ View on GitHub💬 Have questions about this project? Want to see the next part of the tutorial? Drop a comment below! And don't forget to check out my other .NET and WinForms guides.
♥ Here are some online Visual Basic lessons and courses:
No comments:
Post a Comment