Get OneDrive path in Windows

久未见 提交于 2019-12-05 07:52:17
Matt Lengenfelder

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

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.

This works for me (Windows 10 Pro, 1803):

 var oneDrivePath = Environment.GetEnvironmentVariable("OneDriveConsumer");
tabletguy

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);
    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;
        }
    }

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?

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";

Solution source code is here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!