在Windows中获取OneDrive路径

Ale*_*dru 6 .net c# wpf path onedrive

我有一个C#WPF应用程序,我正在尝试找到一种方法来获取Windows中的根OneDrive目录的路径.我该如何以编程方式执行此操作?我在网上搜索过,但找不到任何东西.我希望我能提供一些代码,但我不知道; 我的意思是,我检查了系统环境变量,我在我的机器上找不到任何东西,认为这可能是一个有效的解决方案,但它没有发现任何东西.

小智 15

随着Windows 10的最新更新,微软推出了新的环境变量%OneDrive%,我已经在2017年4月更新了(创作者更新),它就在那里.

  • 如果您的电脑上只有一个 OneDrive 客户端或默认客户端,则为“%OneDrive%”。如果没有,则“%OneDriveConsumer%”用于个人 OneDrive,“%OneDriveCommercial%”用于 OneDrive for Business。 (11认同)

Rex*_*dan 12

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

 var oneDrivePath = Environment.GetEnvironmentVariable("OneDriveConsumer");
Run Code Online (Sandbox Code Playgroud)


Mat*_*der 5

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

我会尝试使用该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);
Run Code Online (Sandbox Code Playgroud)

我还找到了下面的路径:

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_CURRENT_USER \ Software \ Microsoft \ OneDrive”中,值“ UserFolder” (2认同)

小智 5

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

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

在 Windows 10 上测试

Guid FOLDERID_SkyDrive = new Guid("A52BBA46-E9E1-435f-B3D9-28DAA648C0F6");
location = GetKnownFolderPath(FOLDERID_SkyDrive);
Run Code Online (Sandbox Code Playgroud)