Articles → .NET → Working With Dropbox Using C#

Working With Dropbox Using C#





  1. Get the list of files and folder
  2. Create a folder in Dropbox
  3. Delete a folder in Dropbox
  4. Upload a file in Dropbox



Generate Token Key And Token Secret


  1. First of all type this URL in the address bar of your web browser – https://www.dropbox.com/developers/apps
  2. If you are not logged in, then a login screen will appear. Enter your email id and password.
  3. A screen will appear to create an application. On the right hand side of the page click on ‘Create Apps’ (see the following figure)
  4. Picture showing the Create app button for creating the dropbox application
    Click to Enlarge

  5. Enter following details and click on ‘Create app’.
  6. Picture showing selecting the type of application
    Click to Enlarge

  7. Once the application is created Dropbox will create token key and token secret for you.


Picture showing the token key and secret generated for the application
Click to Enlarge




Adding Required Dlls




  1. Nemiro.OAuth.dll (You can download it from https://github.com/alekseynemiro/nemiro.oauth.dll)
  2. Nemiro.OAuth.LoginForms.dll (You can download it from https://github.com/alekseynemiro/Nemiro.OAuth.LoginForms)


using Nemiro.OAuth;
using Nemiro.OAuth.LoginForms;





Get The List Of Files And Folder




public partial class Form3: Form {
  private string CurrentPath = "/";

  public Form3() {
    InitializeComponent();
  }

  private void Form3_Load(object sender, EventArgs e) {
    var login = new DropboxLogin("token key", "token secret");
    login.Owner = this;
    login.ShowDialog();
    if (login.IsSuccessfully) {
      OAuthUtility.GetAsync("https://api.dropbox.com/1/metadata/auto",
        new HttpParameterCollection {
          {
            "path",
            CurrentPath
          }, {
            "access_token",
            login.AccessToken.Value
          }
        }, callback: GetFiles_Result
      );
    }
  }

  //  GetFiles_Result Method
  private void GetFiles_Result(RequestResult result) {
    if (this.InvokeRequired) {

      this.Invoke(new Action < RequestResult > (GetFiles_Result), result);
      return;
    }
    if (result.StatusCode == 200) {
      listBox1.Items.Clear();
      listBox1.DisplayMember = "path";
      foreach(UniValue file in result["contents"]) {
        listBox1.Items.Add(file);
      }
    }
  }
}




Picture showing the login window of the dropbox when the code is executed
Click to Enlarge



Picture showing he confirmation window for providing or cancelling the access on the dropbox account
Click to Enlarge




Create A Folder In Dropbox


string folder_name = "gyan";
var login = new DropboxLogin("token key", "token secret");
login.Owner = this;
login.ShowDialog();
if (login.IsSuccessfully) {
  OAuthUtility.PostAsync(
    "https://api.dropbox.com/1/fileops/create_folder",
    new HttpParameterCollection {
      {
        "access_token",
        login.AccessToken.Value
      }, {
        "root",
        "auto"
      }, {
        "path",
        Path.Combine(this.CurrentPath, folder_name).Replace("\\", "/")
      }
    }, callback: CreateFolder_Result);
}

//  CreateFolder_Result
private void CreateFolder_Result(RequestResult result) {
  if (this.InvokeRequired) {
    this.Invoke(new Action < RequestResult > (CreateFolder_Result), result);
    return;
  }

  if (result.StatusCode == 200) {
    MessageBox.Show("Folder Created Successfully");
  }
}



Delete A Folder In Dropbox


string folder_name = "gyan";
var login = new DropboxLogin("token key", "token secret");
login.Owner = this;
login.ShowDialog();
if (login.IsSuccessfully) {
  OAuthUtility.PostAsync(
    "https://api.dropbox.com/1/fileops/delete",
    new HttpParameterCollection {
      {
        "access_token",
        login.AccessToken.Value
      }, {
        "root",
        "auto"
      }, {
        "path",
        folder_name
      }
    }, callback: DeleteFolder_Result);
}

//  Delete Folder_Result
private void DeleteFolder_Result(RequestResult result) {
  if (this.InvokeRequired) {
    this.Invoke(new Action < RequestResult > (DeleteFolder_Result), result);
    return;
  }

  if (result.StatusCode == 200) {
    MessageBox.Show("Folder Deleted Successfully");
  }
}



Upload A File In Dropbox




string CurrentPath = "/gyan";
var login = new DropboxLogin("token key", "token secret");
login.Owner = this;
login.ShowDialog();
if (login.IsSuccessfully) {
  if (openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK) {
    return;
  }

  OAuthUtility.PutAsync(

    "https://api-content.dropbox.com/1/files_put/auto",
    new HttpParameterCollection {
      {
        "access_token",
        login.AccessToken.Value
      }, {
        "path",
        Path.Combine(this.CurrentPath, Path.GetFileName(openFileDialog1.FileName)).Replace("\\", "/")
      }, {
        "overwrite",
        "true"
      }, {
        "autorename",
        "true"
      }, {
        openFileDialog1.OpenFile()
      }
    },
    callback: UploadFile_Result);

}

//  UploadFile_Result method
private void UploadFile_Result(RequestResult result) {
  if (this.InvokeRequired) {
    this.Invoke(new Action < RequestResult > (UploadFile_Result), result);
    return;
  }

  if (result.StatusCode == 200) {
    MessageBox.Show("File Uploaded Successfully");
  }
}



Posted By  -  Karan Gupta
 
Posted On  -  Saturday, May 9, 2015

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250