See the question and my original answer on StackOverflow

OneDrive is a sync root (aka a Cloud Storage Provider) and you can technically have any number of these for a given installation, so you can use the WinRT's StorageProviderSyncRootManager.GetCurrentSyncRoots Method, something like this, starting with Windows 10, version 1709 "Fall Creators Updade" (2017):

using System;
using Windows.Storage.Provider;

namespace OneDriveList
{
    internal class Program
    {
        static void Main()
        {
            foreach (var root in StorageProviderSyncRootManager.GetCurrentSyncRoots())
            {
                // OneDrive sync roots start with "OneDrive!"
                if (root.Id.StartsWith("OneDrive!"))
                {
                    Console.WriteLine(root.Path.Path);
                }
            }
        }
    }
}

Sample .NET 6 project (note the windows target framework moniker), adapt the version to your needs:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
  </PropertyGroup>

</Project>

For .NET Framework you need to reference the Microsoft.Windows.SDK.Contracts package to access WinRT APIs in Windows.Storage.Provider namespace.

For other languages, this WinRT API is also available if you have proper bindings (aka C++/WinRT for C++, etc.).