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 user's 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 the 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.

  1. /// <summary>  
  2. /// Loads the one drive folders.  
  3. /// </summary>  
  4. /// <returns>IEnumerable<OneDriveFolder>.</returns>  
  5. public static IEnumerable < OneDriveFolder > LoadOneDriveFolders() {  
  6.     const string DisplayNameKey = "DisplayName";  
  7.     const string UserFolderKey = "UserFolder";  
  8.     const string AccountsKey = "Accounts";  
  9.     const string EmailKey = "UserEmail";  
  10.     var folders = new List < OneDriveFolder > ();  
  11.     var oneDriveKey = RegistryHelper.GetCurrentUserRegistryKey(RegistryHelper.KeyCurrentUserOneDrive);  
  12.     if (oneDriveKey.IsNotNull()) {  
  13.         //Get Accounts  
  14.         var accountKey = oneDriveKey.GetSubKey(AccountsKey);  
  15.         if (accountKey.IsNotNull() && accountKey.SubKeyCount > 0) {  
  16.             foreach(var subKeyName in accountKey.GetSubKeyNames()) {  
  17.                 var key = accountKey.GetSubKey(subKeyName);  
  18.                 var folder = new OneDriveFolder();  
  19.                 var directoryValue = key.GetValue < string > (UserFolderKey);  
  20.                 if (directoryValue.HasValue()) {  
  21.                     folder.DirectoryInfo = new DirectoryInfo(directoryValue);  
  22.                     var emailValue = key.GetValue < string > (EmailKey);  
  23.                     if (emailValue.HasValue()) {  
  24.                         folder.UserEmail = emailValue;  
  25.                     }  
  26.                     //Figure out account type   
  27.                     var name = key.GetValue < string > (DisplayNameKey);  
  28.                     if (name.HasValue()) {  
  29.                         folder.AccountType = OneDriveAccountType.Business;  
  30.                         folder.AccountName = name;  
  31.                     } else {  
  32.                         folder.AccountName = key.GetValue < string > (string.Empty);  
  33.                     }  
  34.                     if (folder.AccountName.HasValue() && folder.UserEmail.HasValue()) {  
  35.                         folders.Add(folder);  
  36.                     }  
  37.                 }  
  38.             }  
  39.         }  
  40.     }  
  41.     return folders.AsEnumerable();  
  42. }  

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 user's OneDrive folder, where it favors the first business account it finds.

Ebook Download
View all
Learn
View all
Software architecture, code & app performance, code quality, Microsoft .NET & mentoring. Available!