I have a C# WPF application and I am trying to find a way to get the path to the root OneDrive directory in Windows. How can I do this programmatically? I have searched online, but I couldn't find anything. I wish I could supply some code but I have no clue; I mean, I have checked system environment variables and I couldn't find anything on my machine, thinking that could be a valid solution, but it didn't turn up anything.
13 Answers
With latest update for windows 10, Microsoft introduced new environment variable %OneDrive%, I have checked it on April 2017 update (Creators update) and it is there.

- 611
- 7
- 3
-
10`%OneDrive%` if you have only one OneDrive client on your PC or for the default one. If not, `%OneDriveConsumer%` for the personal OneDrive and `%OneDriveCommercial%` for OneDrive for Business. – Jeankowkow May 24 '21 at 12:05
This works for me (Windows 10 Pro, 1803):
var oneDrivePath = Environment.GetEnvironmentVariable("OneDriveConsumer");

- 398
- 2
- 7
On my Windows 8.1 computer, the registry key that holds this information is: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\SkyDrive\UserFolder
I'd try using the Registry.GetValue()
method:
const string userRoot = "HKEY_CURRENT_USER";
const string subkey = @"Software\Microsoft\Windows\CurrentVersion\SkyDrive";
const string keyName = userRoot + "\\" + subkey;
string oneDrivePath = (string)Registry.GetValue(keyName,
"UserFolder",
"Return this default if NoSuchName does not exist.");
Console.WriteLine("\r\n OneDrivePath : {0}", oneDrivePath);
I also found the path under:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SyncRootManager\SkyDrive\UserSyncRoots\S-1-5-21-2696997101-1021499815-432504798-1004
HKEY_USERS\S-1-5-21-2696997101-1021499815-432504798-1004\Software\Microsoft\Windows\CurrentVersion\SkyDrive\UserFolder

- 7,940
- 7
- 58
- 90

- 719
- 6
- 16
-
I don't recommend your last two paths, you'd additionally need to get your user SID. The last path is technically the first one if the current user had that SID seen there. – Ray Oct 04 '15 at 09:06
-
6Currently it located in `HKEY_CURRENT_USER\Software\Microsoft\OneDrive`, Value `UserFolder` – Alex Zhukovskiy Dec 11 '15 at 14:33
-
I get the location of my OneDrive folder using the constant FOLDERID_SkyDrive (https://msdn.microsoft.com/library/dd378457.aspx) and the "GetKnownFolderPath" method from the answer at // Detect the location of AppData\LocalLow.
Although the environment variable "USERPROFILE" combined with "\OneDrive" will sometimes work, if the user has moved their OneDrive folder, the environment variable will actually be a reparse point, and not the actual location.
Tested on Windows 10
Guid FOLDERID_SkyDrive = new Guid("A52BBA46-E9E1-435f-B3D9-28DAA648C0F6");
location = GetKnownFolderPath(FOLDERID_SkyDrive);
-
C# If I understand correctly, this means it's "automatically" managed code. My full method: [code] public static string GetOneDriveLocation() { var userPath = Environment.GetEnvironmentVariable("USERPROFILE"); var di = new DirectoryInfo(System.IO.Path.Combine(userPath, "OneDrive")); var location = di.FullName; if (di.Attributes.HasFlag(FileAttributes.ReparsePoint)) { Guid FOLDERID_SkyDrive = new Guid("A52BBA46-E9E1-435f-B3D9-28DAA648C0F6"); location = GetKnownFolderPath(FOLDERID_SkyDrive); } return location; }[/code] – tabletguy Oct 23 '15 at 12:48
For completeness, there seem to be 3 environment variables set:
OneDrive
OneDriveConsumer
OneDriveCommercial
In my case, the first and last are the same (my OneDrive for Business account) and the middle one is my personal OneDrive. I'm seeing the same results on both a domain joined PC and a non-domain joined PC but with both OneDrive configured. On a non-domain joined PC with just my personal OneDrive then the OneDrive
environment variable points to the personal OneDrive.
I can't find any Microsoft documentation for this but I think that it may be best to ignore the OneDrive
variable and just use the OneDriveConsumer
/OneDriveCommercial
ones to find OneDrive folders.
Steve

- 1,373
- 10
- 24

- 41
- 2
private void button1_Click(object sender, EventArgs e)
{
try
{
const string userRoot = "HKEY_CURRENT_USER";
const string subkey = @"Software\Microsoft\OneDrive";
const string keyName = userRoot + "\\" + subkey;
string oneDrivePath = (string)Microsoft.Win32.Registry.GetValue(keyName,
"UserFolder",
"Return this default if NoSuchName does not exist.");
Console.WriteLine("\r\n OneDrivePath : {0}", oneDrivePath);
string Onedrivepath= string.Format(oneDrivePath);
label1 .Text = string.Format(Onedrivepath);
}
catch (Exception)
{
/// throw;
}
}

- 149
- 1
- 4
-
1Welcome to SO! Could you provide more information and explanation of the code you posted? – fstanis Oct 25 '16 at 22:09
To keep track of these OneDrive environment variables (It will display all environment variables starting by "one"):
From CMD:
$>set one
OneDrive=C:\Users\my_username\OneDrive - COMPANY
OneDriveCommercial=C:\Users\my_username\OneDrive - COMPANY
OneDriveConsumer=C:\Users\my_username\OneDrive
From PowerShell:
$>dir env: | Where-Object {$_.Name -like "one*"}
OneDrive=C:\Users\my_username\OneDrive - COMPANY
OneDriveCommercial=C:\Users\my_username\OneDrive - COMPANY
OneDriveConsumer=C:\Users\my_username\OneDrive
or
$>Get-ChildItem env: | Where-Object {$_.Name -like "one*"}
OneDrive=C:\Users\my_username\OneDrive - COMPANY
OneDriveCommercial=C:\Users\my_username\OneDrive - COMPANY
OneDriveConsumer=C:\Users\my_username\OneDrive

- 301
- 3
- 13
I was thinking the registry as Smashing1978 mentioned, but I do not have a UserFolder key under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\SkyDrive.
Could you use the %UserProfile%\SkyDrive path?

- 156
- 5
-
What about `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SyncRootManager\SkyDrive\UserSyncRoots\`? – Alexandru Nov 06 '14 at 04:21
-
1I do have an entry in my registry under the key "S-1-5-21-3897053772-1539939438-3998215170-1142" – jasonnissen Nov 06 '14 at 04:31
-
1That is your current user profile's security identifier. I think this is the only known way to detect the OneDrive path as of right now. – Alexandru Nov 06 '14 at 04:37
You must find path under registry ... First run regedit from search box , then under Software - Microsoft - find OneDrive image description here
Then use that path for you subkey string
const string subkey = @"Software\Microsoft\OneDrive";

- 149
- 1
- 4
The registry didn't work for me on some PC's. However, this worked for me:
using System;
using System.IO;
DirectoryInfo di = DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
string path = di.Parent.FullName;

- 1
I'm guilty of only skimming this but see some issues with the questions & answers. The question asks "...I am trying to find a way to get the path to the root OneDrive directory...". I'm using Win11 & Win10 (version 10.0.19044 specifically). Both show the same details below with OneDrive settings
There are 2 paths with OneDrive to consider: The local PC path with cached files/folders & the actual OneDrive path that contains everything. Windows lets users keep all or just selected files/directories cached locally. All of the answers focus on the local path. I am giving an alternate set of details because this opens OneDrive in Explorer rather than a web page or the local cache. This also answers the question being asked but uses OneDrive not the local cache.
Get the value of HKCU\Software\Microsoft\OneDrive\Accounts\Personal value: cid (string value). This is the ID needed for a UNC path. The UNC is: \\d.docs.live.net@SSL\[whatever the registry CID is]. This will get you everything in OneDrive, not just what's cached locally on the PC. You can even map a drive to that UNC path & it works.
I have not compared this with OneDrive @work yet, but I will.
Is the poster looking for the local path or for the full OneDrive path?
For the local cache path, it's here: HKCU\Software\Microsoft\OneDrive\Accounts\Personal value: UserFolder (string value)
Relying on an OS variable ties you to specific versions of Win10 or later which might not be a good idea depending on your delivery audience. What I've described covers the local cache + entire OneDrive.
(you don't need a SID or have to convert a SID to a name & you don't need to use OS variables that won't necessarily be present)

- 139
- 1
- 4