Determine User OneDrive Folders without the Microsoft.OneDriveSDK

Recently, while working on my dotNetTips.Utility Dev App, I realized I was determining the location of the users OneDrive folder wrong, especially if they have more than one OneDrive account, like myself. I didn’t want to go through the hassle of learning the Microsoft.OneDriveSDK, nor did I want to use an entire SDK just to figure this out.

Recently while looking at the Windows Registration Database, I noticed that OneDrive stores this information there. After figuring out the differences between in info stored for personal and business accounts, I came up with a method to retrieve this info for me. Here is what it looks like.

public static IEnumerable<OneDriveFolder> LoadOneDriveFolders()
 {
     const string DisplayNameKey = "DisplayName";
     const string UserFolderKey = "UserFolder";
     const string AccountsKey = "Accounts";
     const string EmailKey = "UserEmail";
 
     var folders = new List<OneDriveFolder>();
 
     var oneDriveKey = RegistryHelper.GetCurrentUserRegistryKey(
                         RegistryHelper.KeyCurrentUserOneDrive);
 
     if (oneDriveKey.IsNotNull())
     {
         //Get Accounts
         var accountKey = oneDriveKey.GetSubKey(AccountsKey);
 
         if (accountKey.IsNotNull() && accountKey.SubKeyCount > 0)
         {
             foreach (var subKeyName in accountKey.GetSubKeyNames())
             {
                 var key = accountKey.GetSubKey(subKeyName);
 
                 var folder = new OneDriveFolder();
                 var directoryValue = key.GetValue<string>(UserFolderKey);
 
                 if (directoryValue.HasValue())
                 {
                     folder.DirectoryInfo = new DirectoryInfo(directoryValue);
 
                     var emailValue = key.GetValue<string>(EmailKey);
 
                     if (emailValue.HasValue())
                     {
                         folder.UserEmail = emailValue;
                     }
 
                     //Figure out account type                           
                     var name = key.GetValue<string>(DisplayNameKey);
 
                     if (name.HasValue())
                     {
                         folder.AccountType = OneDriveAccountType.Business;
                         folder.AccountName = name;
                     }
                     else
                     {
                         folder.AccountName = key.GetValue<string>(string.Empty);
                     }
 
                     if (folder.AccountName.HasValue() && 
                           folder.DirectoryInfo.IsNotNull())
                     {
                         folders.Add(folder);
                     }
                 }
             }
         }
     }
 
     return folders.AsEnumerable();
 }

I wrote this method in .NET Standard, so I had to install the Microsoft.Win32.Registry NuGet package first. Also, you might see some methods that you might not recognize. They are all from my open-source library, that includes the full code for this method, dotNetTips.Utility.Standard on GitHub and also available via a NuGet package.

With this code my app now properly chooses the users OneDrive folder, where it favors the first business account it finds.