J'ai lutté contre l'API Dropbox et j'ai fait mes petits pas (je suis nouveau sur C#). Le fait est que j'ai finalement atteint un fichier sur mon compte Dropbox mais je ne sais pas comment le créer sur ma machine locale via StreamWriter.Comment écrire un fichier Dropbox (via son API) en utilisant StreamWriter en C#?
Note: Je sais que la chose await et ainsi de suite pourrait être facilement améliorée, mais je reçois toujours en elle: «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();
}
}
}
}
}
Le point va à sw.Write, où je « essaie de insérer "la réponse que je reçois sur la console mais je viens d'obtenir ce" System.Threading.Tasks.Task`1 [System.Byte []] "à la place.
Merci d'avance et désolé pour le n00bance. Nouveau sur API C# et Dropbox.
Divisez cet appel en 'var response = wait response ...' et imprimez la propriété que vous voulez imprimer. – Fildor