Articles → .NET → Working With Dropbox Using C#
Working With Dropbox Using C#
- Get the list of files and folder
- Create a folder in Dropbox
- Delete a folder in Dropbox
- Upload a file in Dropbox
Generate Token Key And Token Secret
- First of all type this URL in the address bar of your web browser – https://www.dropbox.com/developers/apps
- If you are not logged in, then a login screen will appear. Enter your email id and password.
- A screen will appear to create an application. On the right hand side of the page click on ‘Create Apps’ (see the following figure)
Click to Enlarge
- Enter following details and click on ‘Create app’.
Click to Enlarge
- Once the application is created Dropbox will create token key and token secret for you.
Click to Enlarge
Adding Required Dlls
- Nemiro.OAuth.dll (You can download it from https://github.com/alekseynemiro/nemiro.oauth.dll)
- 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);
}
}
}
}
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");
}
}