public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
 
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
 
namespace DeleteStoreApp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
 
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
 
private void Button_Click_1(object sender, RoutedEventArgs e)
{
WebRequest request = WebRequest.Create("http://www.bing.com/");
 
request.BeginGetResponse((aResult) =>
{
var retour = aResult.AsyncState as WebRequest;
WebResponse reponse = retour.EndGetResponse(aResult);
callback(reponse);
}, request);
}
 
private void callback(WebResponse reponse)
{
string currentFileName = @"Test.txt";
//string categorie = currentFileName.Split('/').ElementAt(0);
//string dir = currentFileName.Split('/').ElementAt(1);
 
var mem = new MemoryStream();
using (var stream = reponse.GetResponseStream())
{
stream.CopyTo(mem);
}
 
mem.Seek(0L, SeekOrigin.Begin);
WriteFile(currentFileName, mem.ToArray());
}
 
private void WriteFile(string path, IEnumerable<byte> contents)
{
WriteFileCommon(path, (stream) =>
{
using (var binaryWriter = new BinaryWriter(stream))
{
binaryWriter.Write(contents.ToArray());
binaryWriter.Flush();
}
});
}
 
private static void WriteFileCommon(string path, Action<Stream> streamAction)
{
try
{
var storageFile = CreateStorageFileFromRelativePath(path);
var streamWithContentType = storageFile.OpenAsync(FileAccessMode.ReadWrite).Await();
var stream = streamWithContentType.AsStreamForWrite();
streamAction(stream);
}
catch (Exception exception)
{
throw;
}
}
 
 
private static StorageFile CreateStorageFileFromRelativePath(string path)
{
var fullPath = ToFullPath(path);
var directory = Path.GetDirectoryName(fullPath);
var fileName = Path.GetFileName(fullPath);
var storageFolder = StorageFolder.GetFolderFromPathAsync(directory).Await();
var storageFile = storageFolder.CreateFileAsync(fileName).Await();
return storageFile;
}
 
private static string ToFullPath(string path)
{
var localFolderPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
return System.IO.Path.Combine(localFolderPath, path);
}
}
public static class WinRTExtensionMethods
{
public static void Await(this IAsyncAction operation)
{
var task = operation.AsTask();
task.Wait();
if (task.Exception != null)
{
// TODO - is this correct?
throw task.Exception.InnerException;
}
}
 
public static TResult Await<TResult>(this IAsyncOperation<TResult> operation)
{
var task = operation.AsTask<TResult>();
task.Wait();
if (task.Exception != null)
{
// TODO - is this correct?
throw task.Exception.InnerException;
}
 
return task.Result;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.