wpf 在 Windows 中获取 OneDrive 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26771265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:41:06  来源:igfitidea点击:

Get OneDrive path in Windows

c#.netwpfpathonedrive

提问by Alexandru

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.

我有一个 C# WPF 应用程序,我试图找到一种方法来获取 Windows 中 OneDrive 根目录的路径。我怎样才能以编程方式做到这一点?我在网上搜索过,但找不到任何东西。我希望我能提供一些代码,但我不知道;我的意思是,我检查了系统环境变量,但在我的机器上找不到任何东西,我认为这可能是一个有效的解决方案,但它没有出现任何问题。

采纳答案by 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

在我的 Windows 8.1 计算机上,保存此信息的注册表项是: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\SkyDrive\UserFolder

I'd try using the Registry.GetValue()method:

我会尝试使用以下Registry.GetValue()方法:

        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_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

HKEY_USERS\S-1-5-21-2696997101-1021499815-432504798-1004\Software\Microsoft\Windows\CurrentVersion\SkyDrive\UserFolder

回答by mmichtch

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.

对于 Windows 10 的最新更新,Microsoft 引入了新的环境变量 %OneDrive%,我在 2017 年 4 月更新(Creators 更新)中检查了它,它就在那里。

回答by RexCardan

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

这对我有用(Windows 10 Pro,1803):

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

回答by 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.

我使用常量FOLDERID_SkyDrive( https://msdn.microsoft.com/library/dd378457.aspx) 和 // Detect the location of AppData\LocalLow的答案中的“GetKnownFolderPath”方法获取我的 OneDrive 文件夹的位置

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.

虽然环境变量“USERPROFILE”与“\OneDrive”组合有时会起作用,但如果用户移动了他们的 OneDrive 文件夹,环境变量实际上将是一个重新分析点,而不是实际位置。

Tested on Windows 10

在 Windows 10 上测试

Guid FOLDERID_SkyDrive = new Guid("A52BBA46-E9E1-435f-B3D9-28DAA648C0F6");
location = GetKnownFolderPath(FOLDERID_SkyDrive);

回答by Damo Gurcinovski

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

回答by Damo Gurcinovski

You must find path under registry ... First run regedit from search box , then under Software - Microsoft - find OneDrive image description here

您必须在注册表下找到路径...首先从搜索框中运行 regedit,然后在软件 - Microsoft -在此处找到 OneDrive 图像描述

Then use that path for you subkey string

然后为您的子键字符串使用该路径

const string subkey = @"Software\Microsoft\OneDrive";

const string subkey = @"Software\Microsoft\OneDrive";

Solution source code is here

解决方案源代码在这里

回答by jasonnissen

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.

我在想 Smashing1978 提到的注册表,但我在 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\SkyDrive 下没有 UserFolder 键。

Could you use the %UserProfile%\SkyDrive path?

您可以使用 %UserProfile%\SkyDrive 路径吗?

回答by REXXman

in VBA use Environ("OneDriveConsumer")

在 VBA 中使用 Environ("OneDriveConsumer")