Last active
March 24, 2018 01:56
-
-
Save ichiroku11/debef191d44aa86db59e51e79b53af6f to your computer and use it in GitHub Desktop.
Dropbox.NETを試す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Dropbox.Api; | |
namespace ConsoleApp { | |
// Dropboxサンプル | |
class DropboxSample { | |
// Dropboxクライアント | |
private readonly DropboxClient _client; | |
public DropboxSample(DropboxClient client) { | |
_client = client; | |
} | |
// サンプルを実行 | |
public async Task<bool> RunAsync() { | |
// "/Temp/sample"フォルダを作成 | |
await _client.Files.CreateFolderV2Async("/Temp/sample"); | |
// ファイルをアップロード | |
{ | |
// "text1.txt" | |
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("file1 content"))) { | |
await _client.Files.UploadAsync("/Temp/sample/text1.txt", body: stream); | |
} | |
// "text2.txt" | |
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("file2 content"))) { | |
await _client.Files.UploadAsync("/Temp/sample/text2.txt", body: stream); | |
} | |
} | |
// ファイルの一覧を取得 | |
{ | |
// ListFolderContinueAsyncを試したいのでlimitを1にする | |
var result = await _client.Files.ListFolderAsync("/Temp/sample", limit: 1); | |
foreach (var entry in result.Entries) { | |
Console.WriteLine(entry.Name); | |
// text1.txt | |
} | |
while (result.HasMore) { | |
result = await _client.Files.ListFolderContinueAsync(result.Cursor); | |
foreach (var entry in result.Entries) { | |
Console.WriteLine(entry.Name); | |
// text2.txt | |
} | |
} | |
} | |
// ファイルをダウンロード | |
{ | |
// "text1.txt" | |
using (var response = await _client.Files.DownloadAsync("/Temp/sample/text1.txt")) { | |
Console.WriteLine(await response.GetContentAsStringAsync()); | |
// file1 content | |
} | |
// "text2.txt" | |
using (var response = await _client.Files.DownloadAsync("/Temp/sample/text2.txt")) { | |
Console.WriteLine(await response.GetContentAsStringAsync()); | |
// file2 content | |
} | |
} | |
// ファイルを削除 | |
{ | |
// "text1.txt" | |
await _client.Files.DeleteV2Async("/Temp/sample/text1.txt"); | |
// "text2.txt" | |
await _client.Files.DeleteV2Async("/Temp/sample/text2.txt"); | |
} | |
// "/Temp/sample"フォルダを削除 | |
await _client.Files.DeleteV2Async("/Temp/sample"); | |
return true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Dropbox.Api; | |
namespace ConsoleApp { | |
class Program { | |
static void Main(string[] args) { | |
const string token = "ここにアクセストークン"; | |
// アクセストークンを使ってDropboxクライアントを生成 | |
using (var client = new DropboxClient(token)) { | |
// Dropboxサンプルを実行 | |
new DropboxSample(client).RunAsync().Wait(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment