问题
Stream stream = await new HttpClient().GetStreamAsync( url );
How can i create a file in app's local folder having data downloaded through this stream as file content?
回答1:
You could probably do some kind of stream to stream copy. However, I happen to just use the response object in my code:
var client = new HttpClient();
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
Stream stream = null;
StorageFolder localFolder = ApplicationData.Current.TemporaryFolder;
StorageFile file = await localFolder.CreateFileAsync("savename.htm",
CreationCollisionOption.ReplaceExisting);
stream = await file.OpenStreamForWriteAsync();
await response.Content.CopyToAsync(stream);
回答2:
You need to use a StorageFile
Something like this:
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("yourfile.xml", CreationCollisionOption.ReplaceExisting);
More information at http://social.msdn.microsoft.com
来源:https://stackoverflow.com/questions/15599654/how-to-create-a-file-from-system-io-stream-in-metro-apps