Articles → .NET → Working with dropbox using C#

Working with dropbox using C#
















Generate token key and token secret









Click to Enlarge




Click to Enlarge




Click to Enlarge




Adding required DLLs










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);
      }
    }
  }
}





Click to Enlarge




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