如何在没有API的情况下找到OneDrive(SkyDrive)和GoogleDrive文件夹?

3
4个回答

2

OneDrive / SkyDrive

Chiwda的解决方案指引了我正确的方向,但在我的电脑上(Windows 8.1 德语版),该注册表键并不存在,因此不能直接使用。

相反,以下方法可行:

private static string getOneDriveFolderPath()
{
    var value1 = Registry.GetValue(
        @"HKEY_CURRENT_USER\Software\Microsoft\SkyDrive", 
        @"UserFolder", null);

    var path1 = value1 as string;
    if (path1 != null && Directory.Exist(path1)) return path1;

    var value2 = Registry.GetValue(
        @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\SkyDrive",
        @"UserFolder", null);

    var path2 = value2 as string;
    if (path2 != null && Directory.Exists(path2)) return path2;

    var value3 = Registry.GetValue(
        @"HKEY_CURRENT_USER\Software\Microsoft\OneDrive",
        @"UserFolder", null);

    var path3 = value3 as string;
    if (path3 != null && Directory.Exists(path3)) return path3;

    return null;
}

该代码首先尝试Chiwdas答案中的路径,然后尝试我的机器上存在的路径。


1
这个可行。但是我反转了顺序,首先检查#3,因为它可能是最近和当前的值,如果存在多个值,则使用它。 - JnLlnd

2

我在这里找到了部分答案...

如何使用C#编程来定位我的Google驱动器文件夹?

以下是我为三个主要Webfolder服务编写的代码:

Dim StoreFolder As String = ""
' Dropbox
Dim dbPath As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Dropbox\\host.db")
Dim lines() As String = System.IO.File.ReadAllLines(dbPath)
Dim dbBase64Text As Byte() = Convert.FromBase64String(lines(1))
StoreFolder = System.Text.ASCIIEncoding.ASCII.GetString(dbBase64Text)

' SkyDrive
StoreFolder = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\SkyDrive", "UserFolder", Nothing)

' Google Drive
Dim dbPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Google\\Drive\\sync_config.db")
File.Copy(dbPath, "temp.db", True)
StoreFolder = File.ReadAllText("temp.db", System.Text.Encoding.ASCII)
StoreFolder = StoreFolder.Substring(StoreFolder.IndexOf("local_sync_root_pathvalue") + 29)
StoreFolder = StoreFolder.Substring(0, StoreFolder.IndexOf(Char.ConvertFromUtf32(24)))

1
这个不起作用。只是垃圾而不是字符串。这段代码还包含许多错误: 1)永远不要使用那样的文件名!你正在复制到当前目录!你永远不知道应用程序当前访问你的库时什么是当前目录。应用程序可以自己使用当前目录,也可能根本不使用它,然后一切都取决于启动...如果你将其作为sub\sub\app.exe启动,那么你将会遇到问题...每次都会无法控制地抛出temp.db... - Dmitry Gusarov
1
  1. 你正在读取ASCII。 ASCII总是一个警示信号,在使用它之前三思。我确实理解你正在尝试在字节级别上使二进制代码工作,但我不知道你将如何处理我的路径中的象形文字......
  2. 所有这些偏移量和停止字节现在对我来说都不起作用。我收到了一堆垃圾。
- Dmitry Gusarov
1
关于文件路径的工作 - 还要注意的是,在大多数情况下,应用链接包含与应用程序位置相同的“启动位置”。而且,由于隔离和权限问题,您无法在exe文件附近编写文件。很抱歉,但这就是垃圾软件出现在这个世界上的原因。 PS:我所有的评论都是关于GoogleDrive位置的。 - Dmitry Gusarov
"...处理路径中的象形文字。你一定是在开玩笑!:-) 无论如何,这是我为MVP应用程序编写的代码,最终我会将其转换为C ++,并自然更加健壮。顺便说一句,在我的可执行文件所在的位置保留ini文件(即使在最终应用程序中也是如此),我认为那里没有问题。" - Chiwda
1
只要您从程序中编写这些ini文件,以及像您的示例中那样使用相对路径访问它们,就没有问题。但是,我强烈反对发布此类代码,因为很多人会复制粘贴它。如果您将应用程序安装在ProgramFiles中,则非管理员帐户将无法正常工作。请至少使用%tmp%来复制temp.db。 - Dmitry Gusarov
在我的Windows 8上,OneDrive的位置是HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\SkyDrive - Uwe Keim

1

谷歌云盘

再次感谢Chiwda的解决方案指引我正确的方向。为了读取本地文件夹的路径,谷歌云盘需要有一个SQLite库可用。

我的代码:

private static string checkGetGoogleDriveLocation()
{
    var appDataPath = Environment.GetFolderPath(
                          Environment.SpecialFolder.LocalApplicationData);
    var dbPath = Path.Combine(appDataPath, 
                     @"Google\Drive\user_default\sync_config.db");

    if (!File.Exists(dbPath)) return null;

    var tmp = dbPath + Guid.NewGuid();
    File.CopyFile(dbPath, tmp);

    var folderPath = tryGetFolderFromGoogleDriveDB(tmp);
    if (string.IsNullOrEmpty(folderPath) || !Directory.Exists(folderPath)) return null;

    File.Delete(folderPath);

    return folderPath;
}

我已经使用"sqlite-net" NuGet包实现了tryGetFolderFromGoogleDriveDB函数,该包也可以在GitHub上获取到:
private static string tryGetFolderFromGoogleDriveDB(string dbFilePath)
{
    using (var conn = new SQLiteConnection(dbFilePath, SQLiteOpenFlags.ReadOnly))
    {
        var cmd = conn.CreateCommand(
            @"select data_value from data where entry_key='local_sync_root_path'");
        return cmd.ExecuteScalar<string>();
    }
}

请注意,sqlite-net包会使用本地的sqlite3.dll文件,因此您必须确保它存储在与您正在使用此代码的可执行文件相同的文件夹中。

0

虽然它可能不能完全适用于您的需求,但是您可能可以通过反向工程将其应用于VB.net。这是我使用过的一种解决方案,可以将某些文档保存到用户Dropbox文件夹中。它在默认安装位置查找文件夹,如果没有找到,则提示选择文件夹。大多数人都会按照默认位置进行操作,因此很少会出现找不到文件夹的情况。从记忆中来看,SkyDrive和Google Drive也遵循相同的默认位置模式,因此只需进行小的调整即可找到它们所有的文件夹。

Sub finddropbox()
Dim strOS, localdrive, dropboxfolder As String
Dim folderSelect As FileDialog
Dim objWMIService, colOperatingSystems, objOperatingSystem

strComputer = "."    
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

For Each objOperatingSystem In colOperatingSystems
    strOS = objOperatingSystem.Caption
Next

If InStr(strOS, "XP") Then
    localdrive = Environ("USERPROFILE") & "\My Documents\"
Else
    localdrive = Environ("USERPROFILE")
End If

dropboxfolder = localdrive & "Dropbox\"
If Dir(dropboxfolder, vbDirectory) = vbNullString Then
    Set folderSelect = Application.FileDialog(msoFileDialogFolderPicker)
        With folderSelect
            .Title = "Select Dropbox Folder"
            .AllowMultiSelect = False
            .InitialFileName = Environ("SYSTEMDRIVE")
            If .Show = -1 Then
                dropboxfolder = .SelectedItems(1)
            End If
       End With
End If

Debug.Print dropboxfolder

End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接