Dropbox.NET

Dropbox API is a .NET SDK for API v2, which helps you easily integrate Dropbox into your app. The Dropbox .NET SDK is a Portable Class Library that works with multiple platforms, including Windows, Windows Phone, and Mono.

Step 1 Create a WPF Application

Step 2 Install Dropbox.NET SDK in to the project

Step 3 Create App in Dropbox

Step 4 Configure Created App for Access

Step 5 Start Development for API Integration.

DropBoxBase Class

Login Window

XAML

  1. <Window x:Class="DropBoxIntegration.Login"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:DropBoxIntegration"
  7. mc:Ignorable="d" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow"
  8. Title="Login" BorderThickness="2" BorderBrush="#FF333738" Loaded="Window_Loaded">
  9. <Grid>
  10. <Grid.ColumnDefinitions>
  11. <ColumnDefinition Width="457*"/>
  12. <ColumnDefinition Width="30"/>
  13. </Grid.ColumnDefinitions>
  14. <Grid.RowDefinitions>
  15. <RowDefinition Height="30"/>
  16. <RowDefinition Height="131*"/>
  17. </Grid.RowDefinitions>
  18. <Border HorizontalAlignment="Stretch"
  19. VerticalAlignment="Stretch" Margin="2" Grid.Row="1" Grid.ColumnSpan="2" SnapsToDevicePixels="True">
  20. <WebBrowser x:Name="Browser" Navigating="Browser_Navigating"/>
  21. </Border>
  22. </Grid>
  23. </Window>

.CS Class

  1. using Dropbox.Api;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Shapes;
  15. namespace DropBoxIntegration
  16. {
  17. /// <summary>
  18. /// Interaction logic for Login.xaml
  19. /// </summary>
  20. public partial class Login : Window
  21. {
  22. #region Variables
  23. private const string RedirectUri = "https://localhost/authorize";
  24. private string DBAppKey = string.Empty;
  25. private string DBAuthenticationURL = string.Empty;
  26. private string DBoauth2State = string.Empty;
  27. #endregion
  28. #region Properties
  29. public string AccessToken { get; private set; }
  30. public string UserId { get; private set; }
  31. public bool Result { get; private set; }
  32. #endregion
  33. public Login(string AppKey, string AuthenticationURL, string oauth2State)
  34. {
  35. InitializeComponent();
  36. DBAppKey = AppKey;
  37. DBAuthenticationURL = AuthenticationURL;
  38. DBoauth2State = oauth2State;
  39. }
  40. public void Navigate()
  41. {
  42. try
  43. {
  44. if (!string.IsNullOrEmpty(DBAppKey))
  45. {
  46. Uri authorizeUri = new Uri(DBAuthenticationURL);
  47. Browser.Navigate(authorizeUri);
  48. }
  49. }
  50. catch (Exception)
  51. {
  52. throw;
  53. }
  54. }
  55. private void Window_Loaded(object sender, RoutedEventArgs e)
  56. {
  57. Dispatcher.BeginInvoke(new Action(Navigate));
  58. // Navigate();
  59. }
  60. private void Button_Click(object sender, RoutedEventArgs e)
  61. {
  62. try
  63. {
  64. this.Close();
  65. }
  66. catch (Exception)
  67. {
  68. throw;
  69. }
  70. }
  71. private void Browser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
  72. {
  73. if (!e.Uri.AbsoluteUri.ToString().StartsWith(RedirectUri.ToString(), StringComparison.OrdinalIgnoreCase))
  74. {
  75. // we need to ignore all navigation that isn't to the redirect uri.
  76. return;
  77. }
  78. try
  79. {
  80. OAuth2Response result = DropboxOAuth2Helper.ParseTokenFragment(e.Uri);
  81. if (result.State != DBoauth2State)
  82. {
  83. return;
  84. }
  85. this.AccessToken = result.AccessToken;
  86. this.Uid = result.Uid;
  87. this.Result = true;
  88. }
  89. catch (ArgumentException ex)
  90. {
  91. }
  92. finally
  93. {
  94. e.Cancel = true;
  95. this.Close();
  96. }
  97. }
  98. }
  99. }

MainWindow

Xaml

  1. <Window x:Class="DropBoxIntegration.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:DropBoxIntegration"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="321" Width="1024" MinHeight="300" MinWidth="600">
  9. <Grid>
  10. <Grid.RowDefinitions>
  11. <RowDefinition Height="35"/>
  12. <RowDefinition Height="70"/>
  13. <RowDefinition Height="113*"/>
  14. <RowDefinition Height="20"/>
  15. </Grid.RowDefinitions>
  16. <Label Content="Work with your DropBox account" FontSize="18" Foreground="Black" FontWeight="SemiBold" Margin="20,0,5,0"></Label>
  17. <GroupBox x:Name="gbAuthentication" HorizontalAlignment="Stretch" Margin="2" VerticalAlignment="Stretch" Grid.Row="1" Background="White">
  18. <GroupBox.Header>
  19. <Label Content="DropBox Authentication" FontWeight="SemiBold"></Label>
  20. </GroupBox.Header>
  21. <Grid>
  22. <Grid.ColumnDefinitions>
  23. <ColumnDefinition Width="100"/>
  24. <ColumnDefinition Width="100*"/>
  25. <ColumnDefinition Width="100"/>
  26. </Grid.ColumnDefinitions>
  27. <Label Content="App Key: " Grid.Column="0" HorizontalContentAlignment="Right" Margin="2"/>
  28. <TextBox x:Name="txtApiKey" Grid.Column="1" Margin="2" MaxLength="100" Text=""></TextBox>
  29. <Button Content="Authenticate" x:Name="btnApiKey" Grid.Column="2" Margin="2" Click="btnApiKey_Click"></Button>
  30. </Grid>
  31. </GroupBox>
  32. <GroupBox x:Name="gbDropBox" HorizontalAlignment="Stretch" Margin="2" Grid.Row="2" VerticalAlignment="Stretch" Background="White" IsEnabled="false">
  33. <GroupBox.Header>
  34. <Label Content="DropBox Operations" FontWeight="SemiBold"></Label>
  35. </GroupBox.Header>
  36. <Grid>
  37. <Grid.ColumnDefinitions>
  38. <ColumnDefinition Width="05"/>
  39. <ColumnDefinition Width="220*"/>
  40. <ColumnDefinition Width="220*"/>
  41. <ColumnDefinition Width="220*"/>
  42. <ColumnDefinition Width="220*"/>
  43. <ColumnDefinition Width="05"/>
  44. </Grid.ColumnDefinitions>
  45. <Grid.RowDefinitions>
  46. <RowDefinition Height="05"/>
  47. <RowDefinition Height="150*"/>
  48. <RowDefinition Height="05"/>
  49. </Grid.RowDefinitions>
  50. <Button x:Name="btnCreateFolder" Content="Create Folder" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="1" FontSize="14" Click="btnCreateFolder_Click"/>
  51. <Button x:Name="btlUpload" Content="Upload File" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="2" FontSize="14" Click="btlUpload_Click"/>
  52. <Button x:Name="btnDownload" Content="Download File" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="3" FontSize="14" Click="btnDownload_Click"/>
  53. <Button x:Name="btnDelete" Content="Delete File/Directory" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="4" FontSize="14" Click="btnDelete_Click"/>
  54. </Grid>
  55. </GroupBox>
  56. </Grid>
  57. </Window>

.CS File

  1. using Dropbox.Api;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace DropBoxIntegration
  17. {
  18. /// <summary>
  19. /// Interaction logic for MainWindow.xaml
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. #region Variables
  24. private string strAppKey = "[Yor Application App Key]";
  25. private string strAccessToken = string.Empty;
  26. private string strAuthenticationURL = string.Empty;
  27. private DropBoxBase DBB;
  28. #endregion
  29. #region Constructor
  30. public MainWindow()
  31. {
  32. InitializeComponent();
  33. }
  34. #endregion
  35. #region Private Methods
  36. public void Authenticate()
  37. {
  38. try
  39. {
  40. if (string.IsNullOrEmpty(strAppKey))
  41. {
  42. MessageBox.Show("Please enter valid App Key !");
  43. return;
  44. }
  45. if (DBB == null)
  46. {
  47. DBB = new DropBoxBase(strAppKey, "TestApp");
  48. strAuthenticationURL = DBB.GeneratedAuthenticationURL(); // This method must be executed before generating Access Token.
  49. strAccessToken = DBB.GenerateAccessToken();
  50. gbDropBox.IsEnabled = true;
  51. }
  52. else gbDropBox.IsEnabled = false;
  53. }
  54. catch (Exception)
  55. {
  56. throw;
  57. }
  58. }
  59. #endregion
  60. private void btnApiKey_Click(object sender, RoutedEventArgs e)
  61. {
  62. try
  63. {
  64. strAppKey = txtApiKey.Text.Trim();
  65. Authenticate();
  66. }
  67. catch (Exception)
  68. {
  69. throw;
  70. }
  71. }
  72. private void btnCreateFolder_Click(object sender, RoutedEventArgs e)
  73. {
  74. try
  75. {
  76. if (DBB != null)
  77. {
  78. if (strAccessToken != null && strAuthenticationURL != null)
  79. {
  80. if (DBB.FolderExists("/Dropbox/DotNetApi") == false)
  81. {
  82. DBB.CreateFolder("/Dropbox/DotNetApi");
  83. }
  84. }
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. ex = ex.InnerException ?? ex;
  90. }
  91. }
  92. private void btlUpload_Click(object sender, RoutedEventArgs e)
  93. {
  94. try
  95. {
  96. if (DBB != null)
  97. {
  98. if (strAccessToken != null && strAuthenticationURL != null)
  99. {
  100. DBB.Upload("/Dropbox/DotNetApi", "Sample-test.jpg", @"D:\Capture4-test.PNG");
  101. }
  102. }
  103. }
  104. catch (Exception)
  105. {
  106. throw;
  107. }
  108. }
  109. private void btnDownload_Click(object sender, RoutedEventArgs e)
  110. {
  111. try
  112. {
  113. if (DBB != null)
  114. {
  115. if (strAccessToken != null && strAuthenticationURL != null)
  116. {
  117. DBB.Download("/Dropbox/DotNetApi", "Sample-test.jpg", @"D:\", "capture4_dwnld.png");
  118. }
  119. }
  120. }
  121. catch (Exception)
  122. {
  123. throw;
  124. }
  125. }
  126. private void btnDelete_Click(object sender, RoutedEventArgs e)
  127. {
  128. try
  129. {
  130. if (DBB != null)
  131. {
  132. if (strAccessToken != null && strAuthenticationURL != null)
  133. {
  134. DBB.Delete("/Dropbox/DotNetApi");
  135. }
  136. }
  137. }
  138. catch (Exception)
  139. {
  140. throw;
  141. }
  142. }
  143. }
  144. }
That’s it. We are done with the integration. Enjoy Coding!