fix(portable): SharePoint and Teams libraries OneDrive syncs count as sync roots

A library synced with its Sync button lies beside the account's UserFolder
(%USERPROFILE%\<tenant>\<site> - <library>), so a portable copy unpacked
there kept .\data inside a synchronised folder. OneDriveFolders.SyncRoots
also reads HKCU\Software\SyncEngines\Providers\OneDrive\*\MountPoint, where
OneDrive registers every folder it syncs (the location Office reads), and
takes the registry root as a parameter for tests on a scratch key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 19:00:57 +00:00
1 parent e97b0e4f84
commit 689dd74865
2 files changed
+88 -22

No files matched your search

@@ -4,43 +4,62 @@ using Titano.Core.IO;
namespace Titano.Core.Windows.Shell;
/// <summary>Folders synchronised by OneDrive for the current user (personal and work/school accounts).</summary>
/// <summary>
/// Folders synchronised by OneDrive for the current user: personal and work/school accounts, and the SharePoint and Teams
/// libraries synced with their Sync button.
/// </summary>
public static class OneDriveFolders
{
private const string AccountsKey = @"Software\Microsoft\OneDrive\Accounts";
/// <summary>
/// The sync engines' registry, which Office reads to find synchronised files: one subkey per folder OneDrive
/// synchronises ("Personal", each work or school account, and each library synced with its Sync button, which lies
/// beside the account's folder, not in it), its local folder in <c>MountPoint</c>.
/// </summary>
private const string MountPointsKey = @"Software\SyncEngines\Providers\OneDrive";
private static readonly string[] EnvironmentVariables = ["OneDrive", "OneDriveConsumer", "OneDriveCommercial"];
/// <summary>
/// Sync roots from the OneDrive environment variables and <c>HKCU\Software\Microsoft\OneDrive\Accounts\*\UserFolder</c>,
/// as full paths without a trailing separator, distinct (case-insensitive). An account key that cannot be read is
/// skipped: the programs start all the same.
/// Sync roots from the OneDrive environment variables, <c>HKCU\Software\Microsoft\OneDrive\Accounts\*\UserFolder</c>
/// and <c>HKCU\Software\SyncEngines\Providers\OneDrive\*\MountPoint</c>, as full paths without a trailing separator,
/// distinct (case-insensitive). A key that cannot be read is skipped: the programs start all the same.
/// </summary>
public static IReadOnlyList<string> SyncRoots()
public static IReadOnlyList<string> SyncRoots() => SyncRoots(Registry.CurrentUser);
/// <summary><see cref="SyncRoots()"/> with the registry keys read under <paramref name="user"/> instead of the current user's hive.</summary>
public static IReadOnlyList<string> SyncRoots(RegistryKey user)
{
var roots = new List<string>();
foreach (var variable in EnvironmentVariables) Add(roots, Environment.GetEnvironmentVariable(variable));
try
{
using var accounts = Registry.CurrentUser.OpenSubKey(AccountsKey);
if (accounts is not null)
{
foreach (var name in accounts.GetSubKeyNames())
{
using var account = accounts.OpenSubKey(name);
Add(roots, account?.GetValue("UserFolder") as string);
}
}
}
catch (Exception ex) when (ex is SecurityException or IOException or UnauthorizedAccessException)
{
// The environment variables name the usual roots.
}
AddFolders(roots, user, AccountsKey, "UserFolder");
AddFolders(roots, user, MountPointsKey, "MountPoint");
return roots;
}
/// <summary>The sync root that contains <paramref name="path"/>, or null. Both are expected to be full paths.</summary>
public static string? Containing(string path, IEnumerable<string> syncRoots) => DataLocation.SyncRootOf(path, syncRoots);
/// <summary>Adds the folder that value <paramref name="valueName"/> of each subkey of <paramref name="keyPath"/> names.</summary>
private static void AddFolders(List<string> roots, RegistryKey user, string keyPath, string valueName)
{
try
{
using var parent = user.OpenSubKey(keyPath);
if (parent is null) return;
foreach (var name in parent.GetSubKeyNames())
{
using var entry = parent.OpenSubKey(name);
Add(roots, entry?.GetValue(valueName) as string);
}
}
catch (Exception ex) when (ex is SecurityException or IOException or UnauthorizedAccessException)
{
// The environment variables and the other key name the usual roots.
}
}
private static void Add(List<string> roots, string? candidate)
{
if (string.IsNullOrWhiteSpace(candidate) || !Path.IsPathFullyQualified(candidate)) return;