Skip to content

Instantly share code, notes, and snippets.

@slodge
Created November 5, 2012 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slodge/4016898 to your computer and use it in GitHub Desktop.
Save slodge/4016898 to your computer and use it in GitHub Desktop.
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment