Listing all cloud folders in Windows using .NET

 
 
  • Gérald Barré

Windows allows to list the OneDrive folders or folders from other cloud providers. This is done by using the StorageProviderSyncRootManager class. The StorageProviderSyncRootManager class provides a static method called GetCurrentSyncRoots that returns a list of StorageProviderSyncRoot objects, which correspond to the registered cloud folders.

Before using the Windows SDK, you need to update the Target Framework of your project to net8.0-windows10.0.18362.0:

Project.csproj (csproj (MSBuild project file))
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0-windows10.0.18362.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

Then, you can use the StorageProviderSyncRootManager.GetCurrentSyncRoots() method to list the sync roots:

C#
foreach (var root in StorageProviderSyncRootManager.GetCurrentSyncRoots())
{
    Console.WriteLine(root.Id);
    Console.WriteLine(root.DisplayNameResource);
    Console.WriteLine(root.Path.Path);
 }

If you are interested in a specific provider, such as OneDrive, you can filter the sync roots by the ProviderId if set or Id property. In the case of OneDrive, the Id starts with OneDrive. Also, you can check if it's a personal or a business account by parsing the value. The Id format is OneDrive!{CurrentUser SID}!{Account type}|{DriveId}

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?Buy Me A Coffee💖 Sponsor on GitHub