Click here to Skip to main content
Click here to Skip to main content

Run Dropbox as a Service with C#

, 2 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to run a cloud storage application like Dropbox as a windows service, for use with Windows Server 2003 and 2008

Introduction 

Cloud storage applications are great, they save money, time, and they pretty much operate on a set-and-forget basis. If ever the PC fails, just get a new one, install the desktop app, and you're back in hours, not days. The problem is that these applications doesn't work on windows servers, because they will only run on a user account, and as soon as you log out, or reboot the server, the application stops. 

Background

A while back we decided to use Dropbox as a Backup solution on one of our servers. It was a windows 2008 server but it could have been just about any Windows PC. The problem was that Dropbox, and I guess most of these applications are designed to run as an application, under the currently logged in user. This of course posed a bit of a problem when using Dropbox as a server based application, because now it changes from being a user based application to a server based application. There are no such things as user accounts from the perspective of a service. 

Using the code

So Dropbox simply will not work if it has to run as the "Local System" account.  You can however, make a service run as a user account. But first you have to do some preliminary work. So: 

  1. Log into the server as an Administrator or Admin level account (one that can install applications)
  2. Install Dropbox as per normal.
  3. Once installed, it will bring up the little post install wizard. Pop your details in there, and complete the wizard.
  4. Now Dropbox is running and the icon is in the tray. Right click on it, and go to the settings.
  5. Under general, uncheck "show desktop notifications" and "start Dropbox on system startup".
  6. Click OK 
  7. Right click on the icon again and Exit Dropbox. 

You have now prepared Dropbox to be able to be started from this service, and to be able to run in the computers background. The service that this project makes, will simply run under the Administrator account. (The same one you used to install Dropbox with.) 

To do this simply go to:  

Start > Control Panel > Administrative Tools > Services 

Double click this service from this project and tab over to "Log On". Set Log on as to "This account", and enter the account details. (The same one you used to install Dropbox with.) 

Apply these settings, and start the service - That's it!  Dropbox will now be running under the Administrator account, but initialized by the service controller, so when you log off, it will continue to run. 

Let's take a look at the code to make the service: 

using System;
using System.Collections;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;

public partial class Service1 : ServiceBase
{
    private string ServiceNameStr = "Dropbox Service";
    private Process AppProcess;

    public Service1()
    {
        this.ServiceName = ServiceNameStr;
        this.EventLog.Source = ServiceNameStr;
        this.EventLog.Log = "Application";
        this.CanHandlePowerEvent = true;
        this.CanHandleSessionChangeEvent = true;
        this.CanPauseAndContinue = true;
        this.CanShutdown = true;
        this.CanStop = true;
        if (!EventLog.SourceExists(ServiceNameStr))
        {
            EventLog.CreateEventSource(ServiceNameStr, "Application");
        }

        AppProcess = new Process();
    }

    protected override void OnStart(string[] args)
    {
        try
        {
            // You will need to find out where Dropbox is installed.
            string wd = @"C:\Users\Administrator\AppData\Roaming\Dropbox\bin\";
            string fn = @"C:\Users\Administrator\AppData\Roaming\Dropbox\bin\Dropbox.exe";

            AppProcess.StartInfo.WorkingDirectory = wd;
            AppProcess.StartInfo.FileName = fn;
            AppProcess.StartInfo.UseShellExecute = false;
            AppProcess.StartInfo.CreateNoWindow = true; 
            AppProcess.StartInfo.Arguments = "/home"; 
            AppProcess.Start();
        }
        catch (Exception err)
        {
            EventLog.WriteEntry(ServiceNameStr, err.Message, EventLogEntryType.Error);
        }
    }

    protected override void OnStop()
    {
        try
        {
            AppProcess.Close();
        }
        catch (Exception err)
        {
            EventLog.WriteEntry(ServiceNameStr, err.Message, EventLogEntryType.Error);
        }
    }
} 

Known Issues 

There are some issues with running Dropbox as a service - mainly that none of the notifications will show, and the icons don't have their "ticks" on them, so you don't actually know if a file is updated or that files are in progress. I can imagine this is precisely why Dropbox don't have this service already. 

I would also set the service to "Automatic (Delayed Start)" in the start-up type. This causes this service to start about 2 minutes after all the regular automatic services have started. This helps your start-up, as its unnecessary to begin syncing right after the boot. 

Otherwise its been a great success - Cloud Backup... Sorted! 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Conraddewet
Chief Technology Officer
South Africa South Africa
Conrad de Wet

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberridoy1-Sep-13 6:23 
QuestionBit of a problem Pinmemberwvd_vegt5-Jun-13 1:08 
AnswerRe: Bit of a problem PinmemberConraddewet8-Jul-13 23:57 
GeneralMy vote of 5 PinmemberMike Barthold2-Jun-13 21:11 
GeneralMy vote of 5 PinmemberAmitosh Swain2-Jun-13 15:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 3 Jun 2013
Article Copyright 2013 by Conraddewet
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid