Get OneDrive path in Windows

2019-05-08 03:37发布

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.

7条回答
淡お忘
2楼-- · 2019-05-08 03:52

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楼-- · 2019-05-08 03:53

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

查看更多
劳资没心,怎么记你
4楼-- · 2019-05-08 03:56
    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;
        }
    }
查看更多
叛逆
5楼-- · 2019-05-08 04:06

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

 var oneDrivePath = Environment.GetEnvironmentVariable("OneDriveConsumer");
查看更多
Explosion°爆炸
6楼-- · 2019-05-08 04:11

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?

查看更多
爷、活的狠高调
7楼-- · 2019-05-08 04:12

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);
查看更多
登录 后发表回答