我一直在对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