List Dropbox directories and files in C#


Dropbox is a file sharing service. The free Basic version lets you easily share up to 2 GB of files across the internet. The $9.99/month Pro version gives you up to 1 TB and the $15.00/month Business version gives you unlimited storage.

To use the service, Dropbox creates a folder and automatically syncs it with a remote location over the internet. You can give other users access to sub-folders to let them share the files you put there. It’s a pretty easy way too share family photos, business files, or lolcats pictures.

Recently someone asked me how you a C# program can list your Dropbox files. Because Dropbox syncs files in a folder, this is actually quite simple. You just list the files in that folder. The only real tricks are finding that folder and listing its files and subdirectories in a TreeView control. And even those aren’t all that hard.

When the example program starts, it uses the following code to display the default Dropbox folder location in the text box.

// Set the default dropbox directory.
private void Form1_Load(object sender, EventArgs e)
    string home = Environment.GetEnvironmentVariable("USERPROFILE");
    txtStart.Text = Path.Combine(home, "Dropbox");

This code uses GetEnvironmentVariable to read the USERPROFILE environment variable. This should return the user’s “home” directory as in C:\Users\Rod.

The code then uses Path.Combine to tack on Dropbox. The Path.Combine method automatically adds the directory separator if necessary (it is in this example) to get a result similar to C:\Users\Rod\Dropbox.

You can edit the directory in the text box if necessary. Then click the Search button to execute the following code.

private void btnSearch_Click(object sender, EventArgs e)
    SearchDir(trvFiles.Nodes, txtStart.Text);

// List the files and subdirectories of this directory.
private void SearchDir(TreeNodeCollection nodes, string dir_name)
    TreeNode dir_node = nodes.Add(dir_name);
    foreach (string filename in Directory.GetFiles(dir_name))
    foreach (string subdir in Directory.GetDirectories(dir_name))
        SearchDir(dir_node.Nodes, subdir);

The Search button’s Click event handler clears the program’s TreeView and then calls SearchDir passing it the TreeView control’s Nodes collection and the Dropbox directory’s name.

The SearchDir method recursively searches the indicated directory and adds the files it finds to the nodes collection is receives as a parameter.

The method starts by adding the directory it is searching to the nodes collection. It saves the returned TreeNode object that represents the directory in the collection.

Next the code uses Directory.GetFiles to list the files in the directory and adds them to the directory’s TreeNode.

The method finishes by using Directory.GetDirectories to list the directory’s subdirectories and recursively calling itself to search them.

When the method is finished, the TreeView holds all of the subdirectories and files contained in the Dropbox folder.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in directories, files, system, tools and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *