Solved

Get path to dropbox folder on different computer

Posted on 2013-11-12
12
1,844 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 84
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now