我一直在對Dropbox API進行鬥爭,並且已經完成了我的小步驟(我是C#的新手)。問題是我終於到達了我的Dropbox帳戶上的一個文件,但我不知道如何通過StreamWriter在本地機器上創建它。如何在C#中使用StreamWriter編寫Dropbox文件(通過他們的API)?
注:我知道的await用品等可以很容易地改善,但我仍然得到了進去:「d
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dropbox.Api;
namespace Try1Dropbox
{
class Program
{
private const string ApiKey = "$$longChickenToken%%!!xD";
static void Main(string[] args)
{
try
{
var task = Task.Run(async() => await Program.Run());
task.Wait();
}
catch (AggregateException ex)
{
var inner = ex.InnerException;
Console.WriteLine(inner.Message);
Console.ReadKey();
}
}
static async Task Run()
{
using (var dbx = new DropboxClient(ApiKey))
{
var full = await dbx.Users.GetCurrentAccountAsync();
await ListRootFolder(dbx);
Console.WriteLine("{0} - {1}", full.Name.DisplayName, full.Email);
Console.ReadKey();
await Download(dbx, @"/", "remotefile.pdf");
}
}
private static async Task ListRootFolder(DropboxClient dbx)
{
var list = await dbx.Files.ListFolderAsync(string.Empty);
// Show folders, then files
foreach (var item in list.Entries.Where(i => i.IsFolder))
{
Console.WriteLine("D {0}/", item.Name);
}
foreach (var item in list.Entries.Where(i => i.IsFile))
{
Console.WriteLine("F{0,8} {1}", item.AsFile.Size, item.Name);
}
}
private static async Task Download(DropboxClient dbx, string folder, string file)
{
string path = System.IO.Path.Combine(folder, file);
var args = new Dropbox.Api.Files.DownloadArg(path);
using (var response = await dbx.Files.DownloadAsync(args))
{
using (var sw = new System.IO.StreamWriter(@"c:\prueba\localtest.pdf"))
{
Console.WriteLine(await response.GetContentAsStringAsync());
sw.Write(response.GetContentAsByteArrayAsync());
Console.ReadKey();
}
}
}
}
}
點去sw.Write,在那裏我「嘗試插入「我在控制檯上得到的響應,但我只是得到這個」System.Threading.Tasks.Task`1 [System.Byte []]「。
在此先感謝您,併爲此感到遺憾。 C#和Dropbox API新增功能
WriteLine正在打印返回的Task對象的String表示形式。將該調用分解爲'var response = await response ...',然後打印您希望打印的任何屬性。 – Fildor