Get OneDrive path in Windows

2019-05-08 03:18发布

问题:

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.

回答1:

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



回答2:

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.



回答3:

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


回答4:

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?



回答5:

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



回答6:

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

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


回答7:

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