Solved

Get path to dropbox folder on different computer

Posted on 2013-11-12
12
1,928 Views
Last Modified: 2013-11-14
I am looking for a way to programmatically find the path of the "Dropbox" folder within a PC.

I ran accross this code for C+, but don't know how to convert it to VBA, if that is possible at all.

private static string GetDropBoxPath()
    {
        var appDataPath = Environment.GetFolderPath(
                                           Environment.SpecialFolder.ApplicationData);
        var dbPath = Path.Combine(appDataPath, "Dropbox\\host.db");

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

        var lines = File.ReadAllLines(dbPath);
        var dbBase64Text = Convert.FromBase64String(lines[1]);
        var folderPath = Encoding.ASCII.GetString(dbBase64Text);

        return folderPath;
    }
0
Comment
Question by:jb702
  • 5
  • 5
  • 2
12 Comments
 
LVL 84
ID: 39644668
That code looks for the path to the "ApplicationData" folder on the machine, and then combines that with "Dropbox\\host.db", which apparently points to the Dropbox folder.

You can use the Environ command to get the AppData path:

Dim sAppData As String
sAppData = Environ("AppData") & "\DropBox\host.db"

"host.db" is an SQLite database, so you'd have to be able to read that file. You'd need the SQLite drivers installed on the remote machine. Also, if the user left the DropBox folder at the "default" location, you couldn't read from host.db - it would be in <users>\My Documents\DropBox.
0
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 500 total points
ID: 39644902
I don't use drop box, but your code appears to be reading the db file just by decoding the data using base64 decoding (and I see that elsewhere on the web).  I have taken the concepts above and converted them to VBA.  I used the FileSystemObject but there are other ways to access the data, if you like.  I also used XML to decode the base 64.  To use in VBA, you will need to add references to
Microsoft XML, v6.0
Microsoft Scripting RunTime

As LMSConsulting stated, you are assuming a path to the file that does not appear to be the default.  You may have to update that in the code.

' ExpertExchange Question ID28292626
' http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_28292626.html
' Expert: ltlbearand3 [http://www.experts-exchange.com/M_2469312.html]
'
' Requires the following references:
'   MICROSOFT SCRIPTING RUNTIME
'   MICROSOFT XML, V6.0

Public Sub Test()
    Debug.Print GetDropBoxPath
End Sub

Private Function GetDropBoxPath() As String
    
    On Error GoTo GetDropBoxPath_Error:
    
    ' Need a reference to Microsoft Scripting RunTime
    Dim objFSO As Scripting.FileSystemObject
    Dim objDropBox As Scripting.TextStream
    Dim strDropBoxFolder As String
    Dim strData() As String
    
    Set objFSO = New Scripting.FileSystemObject
    
    strDropBoxFolder = Environ("AppData") & "\DropBox\host.db"
    
    If objFSO.FileExists(strDropBoxFolder) Then
        Set objDropBox = objFSO.OpenTextFile(strDropBoxFolder, ForReading)
        objDropBox.SkipLine
        GetDropBoxPath = Base64Decode(objDropBox.ReadLine)
    End If
    
Release:
    On Error Resume Next
    Set objDropBox = Nothing
    Set objFSO = Nothing
    Exit Function

GetDropBoxPath_Error:

    Resume Release
    MsgBox "Procedure = GetDropBoxPath" & vbNewLine & _
        "Error Number = " & Err.Number & vbNewLine & _
        "Error Message = " & Err.Description & vbNewLine, _
        vbCritical Or vbSystemModal, "VBA Error"

End Function


Private Function Base64Decode(ByVal EncodedValue As String) As String

    On Error GoTo Base64Decode_Error:

    ' Uses XML to decode the Base 64 string
    ' Requires Reference to Microsoft XML, v6.0 or higher
    Dim objXMLDoc As MSXML2.DOMDocument60
    Dim objNode As MSXML2.IXMLDOMElement

    Set objXMLDoc = New MSXML2.DOMDocument60
    Set objNode = objXMLDoc.createElement("Base64")
    objNode.DataType = "bin.base64"
    objNode.Text = EncodedValue
    
    ' Convert byte array to string and return the string
    Base64Decode = StrConv(objNode.nodeTypedValue, vbUnicode)

Release:
    Set objNode = Nothing
    Set objXMLDoc = Nothing
    
    Exit Function

Base64Decode_Error:
    MsgBox "Procedure = Base64Decode" & vbNewLine & _
        "Error Number = " & Err.Number & vbNewLine & _
        "Error Message = " & Err.Description & vbNewLine, _
        vbCritical Or vbSystemModal, "VBA Error"
        
    Resume Release
End Function

Open in new window

-Bear
0
 

Author Closing Comment

by:jb702
ID: 39645088
Wow, that is fricken awesome. Thank you. i could imagine i could use this function for others means as well. thank you so much.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 84
ID: 39645208
I tried the code on my machine (with DropBox installed, and with a non-default DropBox folder defined) and it errored out. Did it work for you? Just curious really ...
0
 

Author Comment

by:jb702
ID: 39645237
Yeah, i think mine is in the default location.

"C:\Users\Jbryan\Desktop\Dropbox\"

Was this where yours was at?

I have a users  machine where it is located at

"C:\Users\Joe\Dropbox\"

Hmmm.
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 39646654
Glad it worked for you.

@LMSConsulting - Since I don't have dropbox, I can't test this too well.  I know you are just curious and if you want to pursue getting this work, we can look at it - to make the code better.  If we pursue, I am wondering where it errors at.  I know the Environ("AppData") does not always pull in values on some windows boxes, but it works most of the time and it simpler code.  I wonder if that is returning a blank value and therefore an invalid path.
0
 
LVL 84
ID: 39646703
"C:\Users\Jbryan\Desktop\Dropbox\"

I believe that is the default.

I am wondering where it errors at

Right here:

Base64Decode = StrConv(objNode.nodeTypedValue, vbUnicode)

When that line is run, the value of objNode.nodeTypedValue results in "The parameter is incorrect"
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 39646755
Hmm.  I am able to test that part of the code and it works.  What version of Microsoft XML is referenced in the project?  I was running in Excel 2010 to use that VBA editor.  What are you testing the code in?  

If you add this line of code in sub test, does it work?  [Should put ThisIsATest in the immediate window.
    Debug.Print Base64Decode("VGhpc0lzQVRlc3Q=")

Open in new window


-Bear
0
 
LVL 84
ID: 39647516
XML 6.0, and I'm running this in Access 2010.

If I run that line in the Immediate window, I get "ThisIsATest", so it would seem your Base64Decode routine functions as expected.
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 39647859
It makes me wonder if your host.db is not base64 encoded.  Everything I read is that file is base64 encoded.  I wonder if you have a different db version and if maybe it has changed at some point.
0
 
LVL 84
ID: 39648298
I just installed it a few days ago (I don't really use DropBox), so perhaps it is outdated?
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 39650065
I don't know - or maybe they updated it.  Since I don't use it, I don't really know.  I am fairly certain it has something to do with host.db either being corrupt or encoded in a different manner.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDF to JPG 13 45
Copy a range from 1..n excel sheets to one destination sheet 2 33
Error with a code discussed on this page 5 14
How to remove unwanted words? 34 38
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
An article on effective troubleshooting
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question